underscore源码学习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(function(){
const root = (typeof self === 'object' && self.self === self && self) ||
(typeof global === 'object' && global.global === global && global) ||
this ||
{};

const _ = function(obj) {
if (obj instanceof _) return obj;
if(!(this instanceof _)) return new _(obj);
this._wrapped = obj || []
}
const ArrayProto = Array.prototype;
const push = ArrayProto.push;
if (typeof exports != 'undefined' && !exports.nodeType) {
if (typeof module != 'undefined' && !module.nodeType && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}
_.VERSION = '0.1';

_.each = function (arr, cb) {
let i = 0;
let Len = arr.length;
while(i < Len) {
if (_.isFunction(cb)) {
cb.call(arr[i], arr[i], i)
}
i++
}
return this
}

_.isFunction = function(obj) {
return typeof obj == 'function' || false;
};

_.functions = function(obj) {
const names = [];
for(let key in obj) {
if (_.isFunction(obj[key])) names.push(key);
}
return names.sort();
}

_.mixin = function(obj) {
_.each(_.functions(obj), function(name) {
var func = _[name] = obj[name];
_.prototype[name] = function() {
let fn = null
_.each(Array.from(arguments), item => {
if (!_.isFunction(item)) {
this._wrapped = [...this._wrapped, ...item]
} else {
fn = item
}
})
if (!fn) {
return this
}
let args = [this._wrapped];
push.apply(args, [fn]);
return func.apply(_, args);
};
});
return _;
};
_.mixin(_)
})()
-------------本文结束感谢您的阅读-------------