UnderScore源代碼閱讀1

    讀一下underscore源代碼,用於本身學習,我的理解,若是有不對的地方但願指正,謝謝安全

    我覺着閱讀的順序按照從總體到局部,從架構到細節較好。架構

1.總體架構

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

  整個源代碼就是使用全局變量this做爲調用方執行一個匿名自執行函數,目的就是防止全局對象被污染,經過傳遞this的值傳遞全局變量到函數內部。函數

  

2. 安全的構造函數_

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

而後把_加到全局變量上去:this

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

相關文章
相關標籤/搜索