linked answer 的实际代码是:
var args = Array.prototype.slice.call(arguments, 1);
即“切片”,而不是“拼接”
注>
首先slice方法常用于make a copy of the array it's called on:
var a = ['a', 'b', 'c'];
var b = a.slice(); // b is now a copy of a
var c = a.slice(1); // c is now ['b', 'c']
所以简短的回答是代码基本上是在模拟:
arguments.slice(1); // discard 1st argument, gimme the rest
但是你不能直接这样做。 special arguments object(在所有 JavaScript 函数的执行上下文中可用)虽然是 Array-like,因为它支持通过带有数字键的 [] 运算符进行索引,但它实际上并不是一个 Array;你不能.push 上它,.pop 关闭它,或者.slice 它等等。
代码实现这一点的方式是通过“欺骗”slice 函数(在 arguments 对象上同样不可用)以在arguments 的上下文中运行,通过Function.prototype.call:
Array.prototype.slice // get a reference to the slice method
// available on all Arrays, then...
.call( // call it, ...
arguments, // making "this" point to arguments inside slice, and...
1 // pass 1 to slice as the first argument
)
Array.prototype.slice.call(arguments).splice(1) 完成同样的事情,但对splice(1) 进行了无关调用,该调用从Array.prototype.slice.call(arguments) 返回的数组中删除 元素,该数组从索引1 开始并一直持续到末尾的数组。 splice(1) 在 IE 中不起作用(从技术上讲,它缺少第二个参数,告诉它删除 IE 和 ECMAScript 需要多少项)。