function fn() {
// console.log(arguments);//我们拿到的这个arguments 是个为数组
// console.log(arguments.length);
// console.log(arguments[0]);
//它没有 真正数组的相关方法
//我们将原型对象的slice的this指向用call方法绑定arguments这个伪数组 就可以使用slice这个方法了
console.log(Array.prototype.slice.call(arguments, 0));
//同样我们也可以用apply
console.log(Array.prototype.slice.apply(arguments, [0, 2]));
//我们也可以用bind 但是bind不会立即执行 需要调用一下
let bindfn = Array.prototype.slice.bind(arguments)
let val = bindfn(0, 2)
console.log(val);
console.log(Array.prototype.slice.bind(arguments)(0, 3));
}
fn(1, 2, 3, 4, 5, 6)
只想改变this的指向就用bind
这篇博客探讨了JavaScript函数内部的arguments对象,它虽然类似于数组但不具备数组的方法。通过使用Array.prototype.slice.call或apply,可以将arguments转换为真正的数组,从而能够调用数组的方法。示例中展示了如何利用call和apply来实现切片操作,以及如何通过bind方法预定义参数。这揭示了JavaScript中this上下文的灵活运用。
4万+

被折叠的 条评论
为什么被折叠?



