-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearnUnderscore.js
More file actions
78 lines (64 loc) · 2.63 KB
/
learnUnderscore.js
File metadata and controls
78 lines (64 loc) · 2.63 KB
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
// Shortcut function for checking if an object has a given property directly
// on itself (in other words, not on a prototype).
_.has = function(obj, key) {
return obj != null && hasOwnProperty.call(obj, key);
};
/*
*for-in
*遍历可枚举的属性
*有浏览器的兼容问题 IE< 9
* IE < 9 下不能用 for in 来枚举的 key 值集合
var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
*
* propertyIsEnumerable是检测属性是否可用 for...in 枚举
*
*/
var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString');
//IE < 9 cntt use (for in)
var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
/* Unsuited use because IE < 9 "toString" have problem
_.isFunction = function(obj){
return Object.prototype.toString( obj ).toLowerCase() === "[object function]"
}*/
if(typeof /./ != 'function' && typeof Int8Array != 'object'){
_.isFunction = function(obj){
return typeof obj === "function" || false
}
}
_.contains = _.includes = _.include = function(obj, item, fromIndex, guard){
if (!isArrayLike(obj)) obj = _.values(obj);
/**
* note
*/
if (typeof fromIndex != 'number' || guard) fromIndex = 0;
return _.indexOf(obj, item, fromIndex) >= 0;
}
function collectNonEnumProps(obj, keys) {
var nonEnumIdx = nonEnumerableProps.length;
var constructor = obj.constructor;
var proto = (_.isFunction(constructor) && constructor.prototype) || ObjProto;
//window (obj) // Object (obj)
// Constructor is a special case.
var prop = 'constructor';
if (_.has(obj, prop) && !_.contains(keys, prop)) keys.push(prop);
while (nonEnumIdx--) {
prop = nonEnumerableProps[nonEnumIdx]
if (prop in obj && obj[prop] !== proto[prop] && !_.contains(keys, prop)) {
keys.push(prop);
}
}
}
_.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);
// Ahem, IE < 9.
if (hasEnumBug) collectNonEnumProps(obj, keys);
return keys;
};
问题集中在collectNonEnumProps 函数
第49行 的判断依据和原理是啥
第57行 的判断依据和原理是啥