建设网站证,深圳软件开发有限公司,国外网站导航,上海到北京物流经常看到如下代码#xff1a;
function foo() {let k Array.prototype.shift.call(arguments);console.log(k)
}
foo(11,22) //11
Array.prototype.shift.call(arguments)的作用是#xff1a; 取 arguments 中的第一个参数
一、为啥要这么写#xff0c;不直接使用argume…经常看到如下代码
function foo() {let k Array.prototype.shift.call(arguments);console.log(k)
}
foo(11,22) //11
Array.prototype.shift.call(arguments)的作用是 取 arguments 中的第一个参数
一、为啥要这么写不直接使用arguments.shift()呢? 原因就是arguments不是一个真正的数组而是类数组
arguments是类数组很多数组中的方法arguments都没有
打印 arguments 打印Array.prototype 可以看到Array.prototype上是有shift()方法
总结
因 arguments 没有 shift 方法故使用 Array.prototype.shift 函数 又使用call方法来把this指向arguments,
通俗点说就是argument借shift方法,来获取arguments中的第一个参数