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
| // 1. 类数组对象
const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 4,
3: 33, // ignored by slice() since length is 3
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
// length: 3 length属性在这里被忽略, length之外的3:33也被忽略,因为超出了索引
console.log(Array.prototype.slice.call(arrayLike))
// [2, 3 ,4 ]
// 2.非类数组
const someLike = {
len: 3,
0: 2,
1: 3,
2: 4,
3: 33, // ignored by slice() since length is 3
};
// 没有如期调用
console.log(Array.prototype.slice.call(someLike))
// []
const someLike1 = {
//length: 3
0: 2,
1: 3,
2: 4,
3: 33, // ignored by slice() since length is 3
};
// 可见需要正确的length
console.log(Array.prototype.slice.call(someLike1))
// []
|