LoginSignup
1
1

More than 5 years have passed since last update.

underscoreコードリーディング(matcher)

Posted at

underscoreに詳しくないので、勉強半分でソースコードを読む。

利用するバージョン

underscore.js(v1.8.3)

matcherとは

underscorejs.orgのmatcher

こんな説明。

_.matcher(attrs) Alias: matches

Returns a predicate function that will tell you if a passed in object contains all of the key/value properties present in attrs.

var ready = _.matcher({selected: true, visible: true});
var readyToGoList = _.filter(list, ready);

attrに指定したすべてのkey/valueが、objectに含まれているかのテストを通したい時に使えるような関数を返します。

underscore.matcher

コード的にはこのあたり。

  // Returns a predicate for checking whether an object has a given set of
  // `key:value` pairs.
  _.matcher = _.matches = function(attrs) {
    attrs = _.extendOwn({}, attrs);
    return function(obj) {
      return _.isMatch(obj, attrs);
    };
  };

.matcherと.matchesにエイリアスが張られている。
attrsに指定されたものを.extendOwnを用いてコピーする。
objを引数に持ち、
.isMatchを用いてobjにattrsが含まれているかどうかの結果を返す関数を返す。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1