-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderscore.js
784 lines (740 loc) · 36.7 KB
/
underscore.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
// Underscore.js 1.8.3
// http://underscorejs.org
// (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
(function () {
var root = this; //将this,赋值给root,客户端为window,服务端为`exports`
var previousUnderscore = root._; // 缓存全局变量`_`,在`noConflict`方法中有用到,防止与其他库对 `_`的使用冲突
var ArrayProto = Array.prototype,
ObjProto = Object.prototype,
FuncProto = Function.prototype; //缓存变量,以便压缩
var push = ArrayProto.push,
slice = ArrayProto.slice,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty; //缓存变量,提高查询效率,同时利用压缩
var nativeIsArray = Array.isArray,
nativeKeys = Object.keys,
nativeBind = FuncProto.bind,
nativeCreat = Object.create; // ES5 原生方法,如果浏览器支持,则优先使用
var Ctor = function () {}; //空函数
var _ = function (obj) { //构造函数
if (obj instanceof _) return obj; //如果obj是_实例,直接返回
if (!(this instanceof _)) { //如果不是实例,需要返回实例化对象
return new _(obj);
}
this._wrapped = obj; //将obj赋值给_wrapped属性,供后面_.mixin方法调用
};
if (typeof exports !== 'undefined') { //_暴漏给全局对象,客户端:window._ = _, 服务端: exports._ = _
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}
_.VERSION = '1.8.3'; //版本号
var optimizeCb = function (func, context, argCount) { //内部方法,根据context以及参数数量,返回一些回调,迭代方法
if (context === void 0) return func; // 这里 void 0 === undefined,因为undefined不是保留字,可以被重写
switch (argCount == null ? 3 : argCount) { // 这里switch直接删除也可以,之所以这样写,是因为call比apply快很多,
case 1:
return function (value) {
return func.call(context, value);
};
case 2:
return function (value, other) {
return func.call(context, value, other);
};
case 3:
return function (value, index, collection) {
return func.call(context, value, index, collection);
};
case 4:
return function (accumulator, value, index, collection) {
return func.call(context, accumulator, value, index, collection);
};
}
return function () {
return func.apply(context, arguments);
}
};
var cb = function (value, context, argCount) { //callback的统一处理,应用于collection的每个元素
if (value == null) return _.identity;
if (_.isFunction(value)) return optimizeCb(value, context, argCount);
if (_.isObject(value)) return _.matcher(value);
return _.property(value);
};
_.iteratee = function (value, context) {
return cb(value, context, Infinity);
};
var createAssigner = function (keysFunc, undefinedOnly) { // 用到的函数 _.extend = createAssigner(_.allKeys); _.extendOwn = createAssigner(_.keys); _.defaults = createAssigner(_.allKeys, true)
return function (obj) {
var length = arguments.length;
if (length < 2 || obj == null) return obj;
for (var index = 1; index < length; index++) { // 获取第一个参数除外的对象参数
var source = arguments[index],
keys = keysFunc(source),
l = keys.length;
for (var i = 0; i < l; i++) {
var key = keys[i];
if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key]; // _.defaults中有key 的取以首次出现的, 而_.extend 和 _.extendOwn后面对象的键值对直接覆盖前面的
}
}
return obj;
};
};
var baseCreate = function (prototype) { // 继承函数
if (!_.isObject(prototype)) return {};
if (nativeCreat) return nativeCreat(prototype);
Ctor.prototype = prototype;
var result = new Ctor;
Ctor.prototype = null; // 释放Ctor.prototype的内存,保证Ctor是真正的空函数,这里Ctor.prototype重新分配了地址,并不影响实例result的原型链
return result;
};
var property = function (key) {
return function (obj) {
return obj == null ? void 0 : obj[key];
}
};
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1; // JavaScript做大的数值
var getLength = property('length'); // 用来获取 array 以及 arrayLike 元素的 length 属性值
var isArrayLike = function (collection) { //包括数组、arguments、HTML Collection 以及 NodeList,字符串,函数,以及具有键{length}的对象
var length = getLength(collection);
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
};
// Collection Functions
// --------------------
_.each = _.forEach = function (obj, iteratee, context) { // 与 ES5 Array.prototype.forEach一直
iteratee = optimizeCb(iteratee, context); // 根据 context 确定不同的迭代函数
var i, length;
if (isArrayLike(obj)) {
for (i = 0, length = obj.length; i < length; i++) {
iteratee(obj[i], i, obj);
}
} else { // 如果是对象,遍历处理 values 值
var keys = _.keys(obj);
for (i = 0, length = keys.length; i < length; i++) {
iteratee(obj[keys[i]], keys[i], obj);
}
}
return obj;
};
_.map = _.collect = function (obj, iteratee, context) {
iteratee = cb(iteratee, context); // 根据 context 确定不同的迭代函数
var keys = !isArrayLike(obj) && _.keys(obj), // 如果传参是对象,则获取它的 keys 值数组(短路表达式)
length = (keys || obj).length, // 如果 obj 为对象,则 length 为 key.length, 如果 obj 为数组,则 length 为 obj.length
results = Array(length);
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
results[index] = iteratee(obj[currentKey], currentKey, obj);
}
return results;
};
function createReduce(dir) { //dir === 1 -> _.reduce, dir === -1 -> _.reduceRight
function iterator(obj, iteratee, memo, keys, index, length) {
for (; index >= 0 && index < length; index += dir) {
var currentKey = keys ? keys[index] : index;
memo = iteratee(memo, obj[currentKey], currentKey, obj); // 迭代,返回值供下次迭代调用
}
return memo; // 每次迭代返回值,供下次迭代调用
}
return function (obj, iteratee, memo, context) {
iteratee = optimizeCb(iteratee, context, 4); // 可传入的 4 个参数
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length,
index = dir > 0 ? 0 : length - 1;
if (arguments.length < 3) { // 如果没有指定初始值 ,则把第一个元素指定为初始值
memo = obj[keys ? keys[index] : index];
index += dir; // 确定index的初始值
}
return iterator(obj, iteratee, memo, keys, index, length);
}
}
_.reduce = _.foldl = _.inject = createReduce(1); // 与 ES5 Array.prototype.reduce 的方式类似
_.reduceRight = _.foldr = createReduce(-1); // 与 ES5 Array.prototype.reduceRight 的方式类似
_.find = _.detect = function (obj, predicate, context) { // 寻找数组或者对象中第一个满足条件(predicate 函数返回 true)的元素
var key;
if (isArrayLike(obj)) {
key = _.findIndex(obj, predicate, context); // 如果 obj 是数组,key 为满足条件的下标
} else {
key = _.findKey(obj, predicate, context); // 如果 obj 是对象,key 为满足条件的元素的 key 值
}
if (key !== void 0 && key !== -1) return obj[key];
};
_.filter = _.select = function (obj, predicate, context) {
var results = [];
predicate = cb(predicate, context);
_.each(obj, function (value, index, list) {
if (predicate(value, index, list)) results.push(value);
});
return results;
};
_.reject = function (obj, predicate, context) { // // 寻找数组或者对象中所有不满足条件的元素,并以数组方式返回,所得结果是 _.filter 方法的补集
return _.filter(obj, _.negate(cb(predicate)), context);
};
_.every = _.all = function (obj, predicate, context) {
predicate = cb(predicate, context);
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length;
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
if (!predicate(obj[currentKey], currentKey, obj)) return false; // 如果有一个不能满足 predicate 中的条件则返回 false
}
return true;
};
_.some = _.any = function (obj, predicate, context) {
predicate = cb(predicate, context);
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length;
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
if (predicate(obj[currentKey], currentKey, obj)) return true; // 如果有一个足 predicate 中的条件则返回 true
}
return false;
};
_.contains = _.includes = _.include = function (obj, item, fromIndex, guard) {
if (!isArrayLike(obj)) obj = _.values(obj); // 如果是对象,返回 values 组成的数组
if (typeof fromIndex != 'number' || guard) fromIndex = 0; // fromIndex 表示查询起始位置 ,如果没有指定该参数,则默认从头找起
return _.indexOf(obj, item, fromIndex) >= 0;
};
_.invoke = function (obj, method) {
var args = slice.call(arguments, 2);
var isFunc = _.isFunction(method);
return _.map(obj, function (value) {
var func = isFunc ? method : value[method]; // 如果 method 不是函数,则可能是 obj 的 key 值,而 obj[method] 可能为函数
return func == null ? func : func.apply(value, args);
});
};
_.pluck = function (obj, key) { //map常使用的用例模型的简化版本,即萃取数组对象中某属性值,返回一个数组
return _.map(obj, _.property(key));
};
_.where = function (obj, attrs) { // 返回含有attrs键值对的所有对象
return _.filter(obj, _.matcher(attrs));
};
_.findWhere = function (obj, attrs) { //寻找第一个有指定 key-value 键值对的对象
return _.find(obj, _.matcher(attrs));
};
_.max = function (obj, iteratee, context) {
var result = -Infinity,
lastComputed = -Infinity,
value, computed;
if (iteratee == null && obj != null) { // 如果没有有 iteratee 参数如果是数组,则寻找数组中最大元素 ,如果是对象,则寻找最大 value 值
obj = isArrayLike(obj) ? obj : _.values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value > result) {
result = value;
}
}
} else { // 寻找元素经过迭代后的最值
iteratee = cb(iteratee, context); //lastComputed 保存计算过程中出现的最值
_.each(obj, function (value, index, list) {
computed = iteratee(value, index, list); //经过迭代函数后的值
if (computed > lastComputed || computed == -Infinity && result === -Infinity) { // && 的优先级高于 ||
result = value;
lastComputed = computed;
}
});
}
return result;
};
_.min = function (obj, iteratee, context) { //逻辑类似 _.max
var result = Infinity,
lastComputed = Infinity,
value, computed;
if (iteratee == null && obj != null) {
obj = isArrayLike(obj) ? obj : _.values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value < result) {
result = value;
}
}
} else {
iteratee = cb(iteratee, context);
_.each(obj, function (value, index, list) {
computed = iteratee(value, index, list);
if (computed < lastComputed || computed === Infinity && result === Infinity) {
result = value;
lastComputed = computed;
}
});
}
return result;
};
_.shuffle = function (obj) { // 将数组乱序; 如果是对象,则返回一个数组,数组由对象 value 值构成 ; Fisher-Yates shuffle 算法, 最优的洗牌算法,复杂度 O(n), 乱序不要用 sort + Math.random(),复杂度 O(nlogn),而且,并不是真正的乱序;参考 https://github.com/hanzichi/underscore-analysis/issues/15
var set = isArrayLike(obj) ? obj : _.values(obj); // 如果是对象,则对 value 值进行乱序
var length = set.length;
var shuffled = Array(length); // 乱序后返回的数组副本(参数是对象则返回乱序后的 value 数组)
for (var index = 0, rand; index < length; index++) { //遍历数组元素,将其与之前的任意元素交换
rand = _.random(0, index);
if (rand !== index) shuffled[index] = shuffled[rand];
shuffled[rand] = set[index];
}
return shuffled;
};
_.sample = function (obj, n, guard) {
if (n == null || guard) { //如果没有传N,随机返回一个元素
if (!isArrayLike(obj)) obj = _.values(obj);
return obj[_.random(obj.length - 1)];
}
return _.shuffle(obj).slice(0, Math.max(0, n)); // 随机返回 n 个
};
_.sortBy = function (obj, iteratee, context) {
iteratee = cb(iteratee, context);
return _.pluck( // 根据指定的 key 返回 values 数组
_.map(obj, function (value, index, list) { // 根据指定的 key 返回 values 数组 _.map(obj, function(){}).sort()
return {
value: value,
index: index,
criteria: iteratee(value, index, list) // 元素经过迭代函数迭代后的值
};
}).sort(function (left, right) {
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
return left.index - right.index;
}), 'value');
};
var group = function (behavior) { // behavior 是一个函数参数 _.groupBy, _.indexBy 以及 _.countBy 其实都是对数组元素进行分类,分类规则就是 behavior 函数
return function (obj, iteratee, context) {
var result = {};
iteratee = cb(iteratee, context);
_.each(obj, function (value, index) {
var key = iteratee(value, index, obj); // 经过迭代,获取结果值,存为 key
behavior(result, value, key); // 按照不同的规则进行分组操作, 将变量 result 当做参数传入,能在 behavior 中改变该值
});
return result;
};
}
_.groupBy = group(function (result, value, key) { // 根据 key 值分组 , key 是元素经过迭代函数后的值 或者元素自身的属性值
if (_.has(result, key)) result[key].push(value);
else result[key] = [value];
});
_.indexBy = group(function (result, value, key) { // key 值必须是独一无二的,不然后面的会覆盖前面的, 其他和 _.groupBy 类似
result[key] = value;
});
_.countBy = group(function (result, value, key) { // 各组中的对象的数量的计数
if (_.has(result, key)) result[key]++;
else result[key] = 1;
});
_.toArray = function (obj) {
if (!obj) return [];
if (_.isArray(obj)) return slice.call(obj); // 如果是数组,则返回副本数组
if (isArrayLike(obj)) return _.map(obj, _.identity); // 如果是类数组,则重新构造新的数组
return _.values(obj); // 如果是对象,则返回 values 集合
};
_.size = function (obj) {
if (obj == null) return 0;
return isArrayLike(obj) ? obj.length : _.keys(obj).length; //类数组,返回ength 属性,如果是对象,返回键值对数量
};
_.partition = function (obj, predicate, context) {
predicate = cb(predicate, context);
var pass = [],
fail = [];
_.each(obj, function (value, key, obj) {
(predicate(value, key, obj) ? pass : fail).push(value);
});
return [pass, fail];
};
// Array Functions
// ---------------
_.first = _.head = _.take = function (array, n, guard) {
if (array == null) return void 0;
if (n == null || guard) return array[0]; // 没指定参数 n,则默认返回第一个元素
return _.initial(array, array.length - n); // 如果有参数 n,则返回数组前 n 个元素(组成的数组) 返回前 n 个元素,即剔除后 array.length - n 个元素
};
_.initial = function (array, n, guard) {
return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n)));
};
_.last = function (array, n, guard) {
if (array == null) return void 0;
if (n == null || guard) return array[array.length - 1];
return _.rest(array, Math.max(0, array.length - n)); // 剔除前 array.length - n 个元素
};
_.rest = _.tail = _.drop = function (array, n, guard) {
return slice.call(array, n == null || guard ? 1 : n);
};
_.compact = function (array) { //去掉数组中所有的假值
return _.filter(array, _.identity);
};
var flatten = function (input, shallow, strict, startIndex) { // 递归调用数组,将数组展开 ;shallow => 是否只展开一层 ;strict === true,通常和 shallow === true 配合使用; 表示只展开一层,但是不保存非数组元素(即无法展开的基础类型)
var output = [],
idx = 0;
for (var i = startIndex || 0, length = getLength(input); i < length; i++) {
var value = input[i];
if (isArrayLike(value) && (_.isArray(value) || _.isArgments(value))) {
if (!shallow) value = flatten(value, shallow, strict); // 深度递归展开
var j = 0,
len = value.length; // 此时递归展开到最后一层(没有嵌套的数组了),value 值肯定是一个数组
output.length += len; // 感觉没必要
while (j < len) {
output[idx++] = value[j++]; // 将 value 数组的元素添加到 output 数组中
}
} else if (!strict) { // value 不是数组,是基本类型时
output[idx++] = value;
}
}
return output;
};
_.flatten = function (array, shallow) {
return flatten(array, shallow, false);
};
_.without = function (array) { // 删除指定元素之后返回剩余的数组副本
return _.difference(array, slice.call(arguments, 1));
};
_.uniq = _.unique = function (array, isSorted, iteratee, context) { // 数组去重,如果第二个参数 `isSorted` 为 true,则说明事先已经知道数组有序,程序会跑一个更快的算法(一次线性比较,元素和数组前一个元素比较即可),如果有第三个参数 iteratee,则对数组每个元素迭代, 对迭代之后的结果进行去重
if (!_.isBoolean(isSorted)) { // 没有传入 isSorted 参数 为 _.unique(array, false, undefined, iteratee)
context = iteratee;
iteratee = isSorted;
isSorted = false;
}
if (iteratee !== null) iteratee = cb(iteratee, context); // 如果有迭代函数 则根据 this 指向二次返回新的迭代函数
var result = [];
var seen = [];
for (var i = 0, length = getLength(array); i < length; i++) {
var value = array[i],
computed = iteratee ? iteratee(value, i, array) : value; // 如果指定了迭代函数 , 则对数组每一个元素进行迭代 , 迭代函数传入的三个参数通常是 value, index, array 形式
if (isSorted) { //如果是有序数组,则当前元素只需跟上一个元素对比即可, 用 seen 变量保存上一个元素
if (!i || seen !== computed) result.push(value); // 如果 i === 0,是第一个元素,则直接 push ,否则比较当前元素是否和前一个元素相等
seen = computed; // seen 保存当前元素,供下一次对比
} else if (iteratee) {
if (!_.contains(seen, computed)) { // 如果 seen[] 中没有 computed 这个元素值
seen.push(computed);
result.push(value);
}
} else if (!_.contains(result, value)) {
result.push(value); // 如果不用经过迭代函数计算,也就不用 seen[] 变量了
}
}
return result;
};
_.union = function () { // 返回数组并集
return _.uniq(flatten(arguments, true, true));
};
_.intersection = function (array) { // 返回数组去重后的交集,注意这里参数是第一个数组
var result = [];
var argsLength = arguments.length;
for (var i = 0, length = getLength(array); i < length; i++) {
var item = array[i];
if (_.contains(result, item)) continue;
for (var j = 1; j < argsLength; j++) { // 从第二个数组开始循环
if (!_.contains(arguments[j], item)) break; // 判断其他参数数组中是否都有 item 这个元素
}
if (j === argsLength) result.push(item); // j === argsLength 说明其他参数数组中都有 item 元素
}
return result;
};
_.difference = function (array) {
var rest = flatten(arguments, true, true, 1); // 将 others 数组展开一层
return _.filter(array, function (value) { // // 遍历 array,过滤
return !_.contains(rest, value); // 如果 value 存在在 rest 中,则过滤掉
});
};
_.zip = function () { // 将多个数组中相同位置的元素归类
return _.unzip(arguments);
};
_.unzip = function (array) {
var length = array && _.max(array, getLength).length || 0;
var result = Array(length);
for (var index = 0; index < length; index++) {
result[index] = _.pluck(array, index);
}
return result;
};
_.object = function (list, values) { // 将数组转化为对象
var result = {};
for (var i = 0, length = getLength(list); i < length; i++) {
if (values) {
result[list[i]] = values[i];
} else {
result[list[i][0]] = list[i][1];
}
}
return result;
};
function createPredicateIndexFinder(dir) { // dir === 1 => 从前往后找, dir === 1 => 从后往前找
return function (array, predicate, context) {
var length = getLength(array);
var index = dir > 0 ? 0 : length - 1;
for (; index >= 0 && index < length; index += dir) {
if (predicate(array[index], index, array)) return index; // 找到第一个符合条件的元素, 并且返回小标
}
return -1;
}
}
_.findIndex = createPredicateIndexFinder(1);
_.findLastIndex = createPredicateIndexFinder(-1);
_.sortedIndex = function (array, obj, iteratee, context) { // 通过二分查找,将一个元素插入已排序的数组, 返回该插入的位置下标
iteratee = cb(iteratee, context, 1);
var value = iteratee(obj);
var low = 0,
high = getLength(array);
while (low < high) {
var mid = Math.floor((low + high) / 2);
if (iteratee(array[mid]) < value) low = mid + 1;
else high = mid;
}
return low;
}
function createIndexFinder(dir, predicateFind, sortedIndex) { // API 调用形式 _.indexOf(array, value, [isSorted]) ; _.indexOf(array, value, [fromIndex]) ; _.lastIndexOf(array, value, [fromIndex])
return function (array, item, idx) {
var i = 0,
length = getLength(array);
if (typeof idx == 'number') { // 如果 idx 为 Number 类型 ,则规定查找位置的起始点 , 那么第三个参数不是 [isSorted] ,所以不能用二分查找优化了 ,只能遍历查找
if (dir > 0) { // 正向查找
i = idx >= 0 ? idx : Math.max(idx + length, i); // 重置查找的起始位置
} else { // 反向查找
length = idx >= 0 ? Math.mix(idx + 1, length) : idx + length + 1; // 如果是反向查找,重置 length 属性值
}
} else if (sortedIndex && idx && length) { // 能用二分查找加速的条件, 有序 & idx !== 0 && length !== 0
idx = sortedIndex(array, item); //用 _.sortIndex 找到有序数组中 item 正好插入的位置
return array[idx] === item ? idx : -1; //如果正好插入的位置的值和 item 刚好相等 ,明该位置就是 item 第一次出现的位置 ,返回下标 , 否则即是没找到,返回 -1
}
if (item !== item) { // 如果是NaN类型,那么 item => NaN
idx = predicateFind(slice.call(array, i, length), _.isNaN);
return idx >= 0 ? idx + i : -1;
}
for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) { // O(n) 遍历数组,寻找和 item 相同的元素 ,前面排除了 item 为 NaN 的情况 , 可以放心地用 `===` 来判断是否相等了
if (array[idx] === item) return idx;
}
return -1;
};
}
_.indexOf = createIndexFinder(1, _.findIndex, _.sortedIndex); // _.indexOf(array, value, [isSorted]) 找到数组 array 中 value 第一次出现的位置, 并返回其下标值, 如果数组有序,则第三个参数可以传入 true, 这样算法效率会更高(二分查找); [isSorted] 参数表示数组是否有序, 同时第三个参数也可以表示 [fromIndex] (见下面的 _.lastIndexOf)
_.lastIndexOf = createIndexFinder(-1, _.findLastIndex);
_.range = function (start, stop, step) {
if (stop == null) {
stop = start || 0;
start = 0;
}
step = step || 1;
var length = Math.max(Math.ceil((stop - start) / step), 0);
var range = Array(length);
for (idx = 0; idx < length; idx++, start += step) {
range[idx] = start
}
return range;
}
// Function (ahem) Functions
var executeBound = function (sourceFunc, boundFunc, context, callingContext, args) {
if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args);
var self = baseCreate(sourceFunc.prototype);
var result = sourceFunc.apply(self, args);
if (_.isObject(result)) return result; //如果构造函数返回了对象, 则 new 的结果是这个对象, 返回这个对象
return self;
};
_.bind = function (func, context) {
if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function');
var args = slice.call(arguments, 2);
var bound = function () {
return executeBound(func, bound, context, this, args.concat(slice.call(arguments)));
}
return bound;
};
_.partial = function (func) {
var boundArgs = slice.call(arguments, 1);
var bound = function () {
var position = 0,
length = boundArgs.length;
var args = Array(length);
for (var i = 0; i < length; i++) {
args[i] = boundArgs[i] === _ ? arguments[position++] : boundArgs[i]; // 如果该位置的参数为 _,则用 bound 方法的参数填充这个位置 args 为调用 _.partial 方法的 pre-fill 的参数 & bound 方法的 arguments
}
while (position < arguments.length) args.push(arguments[postion++]); // bound 方法还有剩余的 arguments,添上去
return executeBound(func, bound, this, this, args)
};
return bound;
};
_.bindAll = function (obj) {
var i, length = arguments.length,
key;
if (arguments <= 1) throw new Error('bindAll must be passed function names');
for (i = 1; i < length; i++) {
key = arguments[i];
obj[key] = _.bind(obj[key], obj);
}
return obj;
};
_.memoize = function (func, hasher) {
var memoize = function (key) {
var cache = memoize.cache;
var address = '' + (hasher ? hasher.apply(this, arguments) : key);
if (!_has(cache, address)) cache[address] = func.apply(this, arguments);
return cache[address];
}
memoize.cache = {};
return memoize;
}
_.delay = function (func, wait) { //异步执行函数,执行开销大的计算和无阻塞UI线程的HTML渲染时候非常有用
var args = slice.call(arguments, 2);
return setTimeout(function () {
return func.apply(null, args);
}, wait);
};
_.defer = _.partial(_.delay, _, 1); // 延迟调用function, 类似于setTimeout 0
_.throttle = function (func, wait, options) { // 函数节流
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function () {
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function () {
var now = _.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
_.debounce = function (func, wait, immediate) { // 函数去抖(连续事件触发结束后只触发一次)
var timeout, args, context, timestamp, result;
var later = function () {
var last = _.now() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function () {
context = this;
args = arguments;
timestamp = _.now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
}
_.wrap = function (func, wrapper) {
return _.partial(wrapper, func);
};
_.negate = function (predicate) { // 返回一个 predicate 方法的对立方法,即该方法可以对原来的 predicate 迭代结果值取补集
return function () {
return !predicate.apply(this, arguments);
}
};
_.compose = function () {
var args = arguments;
var start = args.length - 1;
return function () {
var i = start;
var result = args[start].apply(this, arguments);
while (i--) result = args[i].call(this, result);
return result;
};
};
_.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error'], function (name) { // 其他类型判断
_['is' + name] = function (obj) {
return toString.call(obj) === '[object ' + name + ']';
};
});
_.isBoolean = function (obj) {
return obj === true || obj === false || toString.call(obj) === '[object Boolean]';
};
if (!_.isArguments(arguments)) { // 兼容IE9
_.isArguments = function (obj) {
return _.has(obj, 'callee');
};
}
_.isArray = nativeIsArray || function (obj) { //判断是否为数组
return toString.call(obj) === '[object Array]'
};
_.random = function (min, max) { // 生成min - max的随机整数
if (max == null) {
max = min;
min = 0
}
return min + Math.floor(Math.random() * (max - min + 1));
};
_.values = function (obj) { //将一个对象的所有 values 值放入数组中 ,仅限 own properties 上的 values不包括原型链上的并返回该数组
var keys = _.keys(obj);
var length = keys.length;
var values = Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values;
}
_.findKey = function (obj, predicate, context) {
predicate = cb(predicate, context);
var keys = _.keys(obj),
key;
for (var i = 0, length = keys.length; i < length; i++) { // 遍历键值对
if (predicate(obj[key], key, obj)) return key; // 符合条件,直接返回 key 值
}
}
_.property = property;
if (typeof /./ != 'function' && typeof Int8Array != 'object') { // _.isFunction兼容处理
_.isFunction = function (obj) {
return typeof obj == 'function' || false;
};
}
_.isObject = function (obj) { // 判断是否为对象,这里对象包括function 和 object
var type = typeof obj;
return type === 'function' || type == 'object' && !!obj; // 这里排除null
};
_.matcher = _.matches = function (attrs) { // 判断一个给定的对象是否有某些键值对
attrs = _.extendOwn({}, attrs);
return function (obj) {
return _.isMatch(obj, attrs);
}
};
_.isMatch = function (object, attrs) { // 判断obj是否有attrs中的所有的key-value键值对
var keys = _.keys(attrs),
length = keys.length;
if (object == null) return !length;
var obj = Object(object); // 这一步感觉没必要
for (var i = 0; i < length; i++) {
var key = keys[i];
if (attrs[key] !== obj[key] || !(key in obj)) return false;
}
return true;
};
_.keys = function (obj) {
if (!_.isObject(obj)) return [];
if (nativeKeys) return nativeKeys(obj);
var keys = [];
for (var key in obj)
if (_has(obj, key)) keys.push(key);
return keys;
};
_.has = function (obj, key) { //判断对象中是否有指定 key
return obj != null && hasOwnProperty.call(obj, key); // 这里是防止hasOwnProperty 作为属性带来问题, 参见https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
};
_.identity = function (value) { // 返回传入的参数
return value
}
_.extendOwn = _.assign = createAssigner(_.keys); //跟 extend 方法类似,但是只把 own properties 拷贝给第一个参数对象 只继承 own properties 的键值对参数个数 >= 1
_.now = Date.now || function () {
return new Date().getTime();
}
}.call(this)); // 设置匿名函数的context外层全局变量,浏览器环境为window.