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 ...
随机推荐
- 乘风破浪:LeetCode真题_025_Reverse Nodes in k-Group
乘风破浪:LeetCode真题_025_Reverse Nodes in k-Group 一.前言 将一个链表按照一定的长度切成几部分,然后每部分进行翻转以后再拼接成一个链表是比较困难的,但是这也能锻 ...
- Mapreduce运行过程分析(基于Hadoop2.4)——(二)
4.3 Map类 创建Map类和map函数.map函数是org.apache.hadoop.mapreduce.Mapper类中的定义的,当处理每一个键值对的时候,都要调用一次map方法,用户须 ...
- Scala学习之路 (九)Scala的上界和下届
一.泛型 1.泛型的介绍 泛型用于指定方法或类可以接受任意类型参数,参数在实际使用时才被确定,泛型可以有效地增强程序的适用性,使用泛型可以使得类或方法具有更强的通用性.泛型的典型应用场景是集合及集合中 ...
- vagrant特性——基于docker开发环境(docker和vagrant的结合)-4-简单例子-有问题
运行一个十分简单的例子: Vagrant.configure() do |config| config.vm.provider "docker" do |d| d.image = ...
- find和find_if
find函数 是在一个迭代器范围内查找特定元素得函数,可将将他用于任意容器类型得元素.这个函数返回的是所找元素得引用,如果没有找到元素就会返回这个容器得尾迭代器. #include <iostr ...
- OpenCV——LBP(Local Binary Patterns)特征检测
#include <opencv2/opencv.hpp> #include <iostream> #include "math.h" using name ...
- JS图片Switchable切换大集合
JS图片切换大集合 利用周末2天把JS图片切换常见效果封装了下,比如:轮播,显示隐藏,淡入淡出等.废话不多说,直接看效果吧!JSFiddler链接如下: 想看JS轮播切换效果请点击我! 当然由于上传图 ...
- A. Nephren gives a riddle
What are you doing at the end of the world? Are you busy? Will you save us? Nephren is playing a gam ...
- 【MongoDB】MongoDB的下载 安装 配置及使用
windows系统 教程 1.下载地址 (官方提供根据系统位数选择对应的bit.exe下载) 由于自己win32系统不支持该官方版本,在网上又找了个 mongodb-win32-i386版本 p ...
- Redis Replication
Replication 官网说明:http://www.redis.io/topics/replication Redis使用异步复制; 一个Master可以有多个Slaves; Slaves可以接收 ...