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';