LoginSignup
2
2

More than 5 years have passed since last update.

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

Posted at

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

利用するバージョン

underscore.js(v1.8.3)

mixinとは

underscorejs.orgのmixin

こんな説明。

_.mixin(object)

Allows you to extend Underscore with your own utility functions.
Pass a hash of {name: function} definitions to have your functions added to the Underscore object, as well as the OOP wrapper.

_.mixin({
  capitalize: function(string) {
    return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
  }
});
_("fabio").capitalize();
=> "Fabio"

Underscoreに貴方自身が使いたい関数を追加することを許可します。
OOPのラッパーと同様に、{name:function}の形で定義されたハッシュを渡すことで、あなたの追加したい関数をUnderscoreのobjectに追加できます。

underscore.mixin

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


  // Add your own custom functions to the Underscore object.
  _.mixin = function(obj) {
    _.each(_.functions(obj), function(name) {
      var func = _[name] = obj[name];
      _.prototype[name] = function() {
        var args = [this._wrapped];
        push.apply(args, arguments);
        return result(this, func.apply(_, args));
      };
    });
  };

.eachを利用する。
第一引数は
.functionsにobjを渡したもの、第二引数にnameを引数とした関数。
この関数の中身は以下
funcに_nameを代入する。
_のprototype[name]に以下の関数を代入する。
argsに[this._wrapped]を代入する。
argsにargumentsをpushする。
resultにthis,funcを渡した結果を返す。

resultは以下

  // Helper function to continue chaining intermediate results.
  var result = function(instance, obj) {
    return instance._chain ? _(obj).chain() : obj;
  };

instance.chainの結果がtrueだった場合(obj).chainの実行結果を、falseの場合はobjを返す。

_chainは_.chainで定義されている

2
2
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
2
2