UnderScore源代码阅读1

    读一下underscore源代码,用于自己学习,个人理解,如果有不对的地方希望指正,谢谢

    我觉着阅读的顺序按照从整体到局部,从架构到细节较好。

1.整体架构

  (function() {}.call(this));

  整个源代码就是使用全局变量this作为调用方执行一个匿名自执行函数,目的就是防止全局对象被污染,通过传递this的值传递全局变量到函数内部。

  

2. 安全的构造函数_

var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};

然后把_加到全局变量上去:

if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}

 

posted @ 2016-05-17 17:04  mylifeholdon  阅读(209)  评论(0编辑  收藏  举报