Underscore.js

Underscore.js 1.1.6のソースコードを見ていく

Underscore.jsの使い方を知るのと、このソースコードリーディングの説明などの部分は以下を参考、引用しています

Underscore.jsはCoffeeScriptで書かれていて、コメントはMarkdownが使われてる(=doccoで表示するため)

Table of Contentsによるとそれぞれジャンル分けされているので、それに沿って読む。

最初の初期化とか、取り決めらへん

グローバル変数

Underscore.jsは_というグローバル変数のみを持っていて、_以下に便利な関数を 詰め込んだ感じのライブラリです。

(function() {
  // Establish the root object, `window` in the browser, or `global` on the server.
  // rootになるオブジェクト、ブラウザだとwindowだけどunderscore.jsはブラウザ以外でも使えるのでthisでグローバルをとる
  var root = this;

  // Save the previous value of the `_` variable.
  // グローバル._を使う
  var previousUnderscore = root._;

  // Establish the object that gets returned to break out of a loop iteration.
  var breaker = {};

  /* 中身 */
})();

全体を即時実行関数にして、thisでグローバルをとってそこに追加していくという感じになっている。 ライブラリのグローバル変数を調べるにはGlobal is the new privateが便利です。

// Create a safe reference to the Underscore object for use below.
var _ = function(obj) { return new wrapper(obj); };

_自体を実行したときもラップしたのが返ってくる

_(obj);// new wrapper(obj);

CommonJS

CommonJS的なルールに沿ってmodule.exports = _;をしておいてる。 Node.jsとかその辺でも使えるようにするための配慮かな

  // Export the Underscore object for **CommonJS**, with backwards-compatibility
  // for the old `require()` API. If we're not in CommonJS, add `_` to the
  // global object.
  if (typeof module !== 'undefined' && module.exports) {
    module.exports = _;
    _._ = _;
  } else {
    root._ = _;
  }

次は Collections

Underscore.js の関連ページ