underscore.js源码解析(一)
一直想针对一个框架的源码好好的学习一下编程思想和技巧,提高一下自己的水平,但是看过一些框架的源码,都感觉看的莫名其妙,看不太懂,最后找到这个underscore.js由于这个比较简短,一千多行,而且读起来容易一些,所以就决定是它了,那废话不多说开始我们的源码学习。
underscore.js源码GitHub地址: https://github.com/jashkenas/underscore/blob/master/underscore.js结构解析
- (function() {
- // 创建一个root对象,在浏览器中表示为window(self)对象,在Node.js中表示global对象,
- // 之所以用用self代替window是为了支持Web Worker
- var root = typeof self == 'object' && self.self === self && self ||
- typeof global == 'object' && global.global === global && global ||
- this;
- // 保存"_"(下划线变量)被覆盖之前的值
- var previousUnderscore = root._;
- // 原型赋值,便于压缩
- var ArrayProto = Array.prototype, ObjProto = Object.prototype;
- // 将内置对象原型中的常用方法赋值给引用变量,以便更方便的引用
- var push = ArrayProto.push,
- slice = ArrayProto.slice,
- toString = ObjProto.toString,
- hasOwnProperty = ObjProto.hasOwnProperty;
- // 定义了一些ECMAScript 5方法
- var nativeIsArray = Array.isArray,
- nativeKeys = Object.keys,
- nativeCreate = Object.create;
- //跟神马裸函数有关,我也不清楚什么意思,有知道可以告诉我
- var Ctor = function(){};
- // 创建一个下划线对象
- var _ = function(obj) {
- // 如果在"_"的原型链上(即_的prototype所指向的对象是否跟obj是同一个对象,要满足"==="的关系)
- if (obj instanceof _) return obj;
- // 如果不是,则构造一个
- if (!(this instanceof _)) return new _(obj);
- // 将underscore对象存放在_.wrapped属性中
- this._wrapped = obj;
- };
- // 针对不同的宿主环境, 将Undersocre的命名变量存放到不同的对象中
- if (typeof exports != 'undefined' && !exports.nodeType) {//Node.js
- if (typeof module != 'undefined' && !module.nodeType && module.exports) {
- exports = module.exports = _;
- }
- exports._ = _;
- } else {//浏览器
- root._ = _;
- }
- //版本号
- _.VERSION = '1.8.3';
- //下面是各种方法以后的文章中会具体说明
- .
- .
- .
- .
- .
- .
- // 创建一个chain函数,用来支持链式调用
- _.chain = function(obj) {
- var instance = _(obj);
- //是否使用链式操作
- instance._chain = true;
- return instance;
- };
- // 返回_.chain里是否调用的结果, 如果为true, 则返回一个被包装的Underscore对象, 否则返回对象本身
- var chainResult = function(instance, obj) {
- return instance._chain ? _(obj).chain() : obj;
- };
- // 用于扩展underscore自身的接口函数
- _.mixin = function(obj) {
- //通过循环遍历对象来浅拷贝对象属性
- _.each(_.functions(obj), function(name) {
- var func = _[name] = obj[name];
- _.prototype[name] = function() {
- var args = [this._wrapped];
- push.apply(args, arguments);
- return chainResult(this, func.apply(_, args));
- };
- });
- };
- _.mixin(_);
- // 将Array.prototype中的相关方法添加到Underscore对象中, 这样Underscore对象也可以直接调用Array.prototype中的方法
- _.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
- //方法引用
- var method = ArrayProto[name];
- _.prototype[name] = function() {
- // 赋给obj引用变量方便调用
- var obj = this._wrapped;
- // 调用Array对应的方法
- method.apply(obj, arguments);
- if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0];
- //支持链式操作
- return chainResult(this, obj);
- };
- });
- // 同上,并且支持链式操作
- _.each(['concat', 'join', 'slice'], function(name) {
- var method = ArrayProto[name];
- _.prototype[name] = function() {
- //返回Array对象或者封装后的Array
- return chainResult(this, method.apply(this._wrapped, arguments));
- };
- });
- //返回存放在_wrapped属性中的underscore对象
- _.prototype.value = function() {
- return this._wrapped;
- };
- // 提供一些方法方便其他情况使用
- _.prototype.valueOf = _.prototype.toJSON = _.prototype.value;
- _.prototype.toString = function() {
- return '' + this._wrapped;
- };
- // 对AMD支持的一些处理
- if (typeof define == 'function' && define.amd) {
- define('underscore', [], function() {
- return _;
- });
- }
- }());
总结
之后的文章中,我会针对underscore中的方法进行具体解析,感谢大家的观看,也希望能够和大家互相交流学习,有什么分析的不对的地方欢迎大家批评指出
underscore.js源码解析(一)的更多相关文章
- underscore.js源码解析(五)—— 完结篇
最近公司各种上线,所以回家略感疲惫就懒得写了,这次我准备把剩下的所有方法全部分析完,可能篇幅过长...那么废话不多说让我们进入正题. 没看过前几篇的可以猛戳这里: underscore.js源码解析( ...
- underscore.js源码解析(四)
没看过前几篇的可以猛戳这里: underscore.js源码解析(一) underscore.js源码解析(二) underscore.js源码解析(三) underscore.js源码GitHub地 ...
- underscore.js源码解析(三)
最近工作比较忙,做不到每周两篇了,周末赶着写吧,上篇我针对一些方法进行了分析,今天继续. 没看过前两篇的可以猛戳这里: underscore.js源码解析(一) underscore.js源码解析(二 ...
- underscore.js源码解析(二)
前几天我对underscore.js的整体结构做了分析,今天我将针对underscore封装的方法进行具体的分析,代码的一些解释都写在了注释里,那么废话不多说进入今天的正文. 没看过上一篇的可以猛戳这 ...
- underscore.js源码解析【'_'对象定义及内部函数】
(function() { // Baseline setup // -------------- // Establish the root object, `window` (`self`) in ...
- underscore.js源码解析【对象】
// Object Functions // ---------------- // Keys in IE < 9 that won't be iterated by `for key in . ...
- underscore.js源码解析【函数】
// Function (ahem) Functions // ------------------ // Determines whether to execute a function as a ...
- underscore.js源码解析【数组】
// Array Functions // --------------- // Get the first element of an array. Passing **n** will retur ...
- underscore.js源码解析【集合】
// Collection Functions // -------------------- // The cornerstone, an `each` implementation, aka `f ...
随机推荐
- 启动android monitor报错解决办法
再这汇总一下这段时间使用android monitor新遇到的问题,特汇总对应问题解决办法如下: 1.确保JDK和Android studio位数相同,比如JDK使用的是64位,studio也要是64 ...
- js实现千位分隔符——str.replace()用法
/*js*/function commafy(num){ return num && num.toString().replace(/(\d{1,3})(?=(\d{3})+(?:$| ...
- Tengine 2.1.2 (nginx/1.6.2)安装配置,淘宝 Web 服务器
简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很 ...
- 【转】ajax 跨域 headers JavaScript ajax 跨域请求 +设置headers 实践
解决跨域调用服务并设置headers 主要的解决方法需要通过服务器端设置响应头.正确响应options请求,正确设置 JavaScript端需要设置的headers信息 方能实现. 此处手札 供后人参 ...
- css 文本溢出
多行文本溢出处理: display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; // 3 行 overflow ...
- MetaMask/metamask-extension-provider
用来探测你的浏览器中有没有安装metamask插件 https://github.com/MetaMask/metamask-extension-provider MetaMask Extension ...
- RedHat 7.3 Oracle 12.2.0.1 RAC 安装手册(转)
1 准备工作 1.1 关于GRID的一些变化 1.1.1 简化的基于映像的Oracle Grid Infrastructure安装 从Oracle Grid Infrastructure 12 ...
- 8.UDP协议
传输层协议:TCP UDP TCP和UDP有什么区别? TCP是面向连接的 UDP是面向无连接.在互通之前,面向连接的协议会先建立连接,如TCP会三次握手.所谓的建立连接,是为了在客户端和服务端维护连 ...
- 动态代理实现设置tomcat请求编码
1)htmlcode: <html> <head> <title>$Title$</title> </head> <body> ...
- Java中 Vector的使用详解
Vector 可实现自动增长的对象数组. java.util.vector提供了向量类(Vector)以实现类似动态数组的功能. 创建了一个向量类的对象后,可以往其中随意插入不同类的对象,即不需顾及类 ...