Array.prototype.xxx.call(arguments)的作用

99 阅读1分钟

在一些函数中,我们经常会看到这样的一行代码 Array.prototype.xxx.call(arguments)

例如

function getArguments(){
    console.log(arguments)
    Array.prototype.shift.call(arguments)
    console.log(arguments)
}
getArguments(1,2)
// 分别打印1,2   和  2

原理是 由于arguments是伪数组形式,没有数组的shift等方法, 所以通过call修改this指向,进而调用数组的方法