ikemonn's blog

技術ネタをちょこちょこと

【Underscore.js】_.matcherを読んだ

jashkenas/underscore_.matcherを読んだ。

概要

_.matcher(attrs)

引数の「key : val」と同じものがあるかを判別する部分適用した関数を返す。

var matcher = _.matcher({age: 20, sex: "male"});
console.log(matcher({age: 20, sex: "male", country: "JP", name: "HOGA"})); // true

ソースコード

_.matcher = _.matches = function(attrs) {
    attrs = _.extendOwn({}, attrs);
    return function(obj) {
      return _.isMatch(obj, attrs);
    };
  };

引数をshallow-copyして、それを_.isMatchに渡した関数を返している。

参考

jashkenas/underscore