[].slice.call

缘由

  • 起源于《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规格

摘录备注三

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这样实现是有意为之,考虑的就是它的扩展性和通用性,我们可以有意的进行扩展和转移

转载于:https://juejin.im/post/5cd3f78bf265da03b91832a2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值