Underscore是一個(gè) JavaScript 工具庫,它提供了一整套函數(shù)式編程的實(shí)用功能,但是沒有擴(kuò)展任何 JavaScript 內(nèi)置對(duì)象寿羞。 他解決了這個(gè)問題:“如果我面對(duì)一個(gè)空白的 HTML 頁面,并希望立即開始工作,我需要什么打厘?” 他彌補(bǔ)了jQuery沒有實(shí)現(xiàn)的功能,同時(shí)又是Backbone必不可少的部分贺辰。
首先分析結(jié)構(gòu)部分
(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';