缘由
- 起源于《es6标准入门》数组的扩展中
[].slice.call(arrayLike)
原理分析
slice
底层实现就是使用循环实现的
var arr = [];
let arrayLike={
0:'a',
1:'b',
2:'c',
length:3
}
for (var i = 0; i < arrayLike.length; i++) {
arr.push(arrayLike[i]);
}
console.log('原理=>',arr);
//等价写法
console.log('快捷=>',[].slice.call(arrayLike));
复制代码
slice规格
- 从js规格看原理
- slice规格链接
摘录备注三
The slice function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.
复制代码
- 意思就是,
slice
这样实现是有意为之,考虑的就是它的扩展性和通用性,我们可以有意的进行扩展和转移