underscore 源码解析(1)

Underscore是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象。 他解决了这个问题:“如果我面对一个空白的 HTML 页面,并希望立即开始工作,我需要什么?” 他弥补了jQuery没有实现的功能,同时又是Backbone必不可少的部分。

首先分析结构部分

(function() {

//Baseline setup

//--------------

//Establish the root object, `window` (`self`) in the browser, `global`

//on the server, or `this` in some virtual machines. We use `self`

//instead of `window` for `WebWorker` support.

varroot=typeofself=='object'&&self.self===self&&self||

typeofglobal=='object'&&global.global===global&&global||

this||

{};

//Save the previous value of the `_` variable.

varpreviousUnderscore=root._;

//Save bytes in the minified (but not gzipped) version:

varArrayProto=Array.prototype, ObjProto=Object.prototype;

varSymbolProto=typeofSymbol!=='undefined'?Symbol.prototype:null;

//Create quick reference variables for speed access to core prototypes.

varpush=ArrayProto.push,

slice=ArrayProto.slice,

toString=ObjProto.toString,

hasOwnProperty=ObjProto.hasOwnProperty;

//All **ECMAScript 5** native function implementations that we hope to use

//are declared here.

varnativeIsArray=Array.isArray,

nativeKeys=Object.keys,

nativeCreate=Object.create;

//Naked function reference for surrogate-prototype-swapping.

varCtor=function(){};

//Create a safe reference to the Underscore object for use below.

var_=function(obj) {

if(objinstanceof_)returnobj;

if(!(thisinstanceof_))returnnew_(obj);

this._wrapped=obj;

};

//Export the Underscore object for **Node.js**, with

//backwards-compatibility for their old module API. If we're in

//the browser, add `_` as a global object.

//(`nodeType` is checked to ensure that `module`

//and `exports` are not HTML elements.)

if(typeofexports!='undefined'&&!exports.nodeType) {

if(typeofmodule!='undefined'&&!module.nodeType&&module.exports) {

exports=module.exports=_;

}

exports._=_;

}else{

root._=_;

}

//Current version.

_.VERSION='1.8.3';

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作者 南京 荆周栋 导师 刘艳 袁浩 郑鹏 解说 一副有关八下历史的一个单元的导图
    bighole阅读 247评论 0 0
  • 智慧源于理念,细枝末节的那不叫智慧,那叫技术,比如说如果你真的要成全孩子的发展,那么你会少一些怒火,多一些宽容,有...
    大橙子5996阅读 212评论 0 0
  • 文|啊惜 当情绪受了自我挑拨,心里荒原噬了绿洲,只觉尽一场荒凉。 古人云:伤春悲秋。遇春逝伤春,处秋来悲秋,所以有...
    赴里阅读 431评论 0 3
  • 《步行者日记》书中,作者从实施步行5000英里计划实施的第一天,就在炎热夏天的行走中,与水疱作斗争,以争取完成当天...
    MikaGO阅读 184评论 0 0