我明白這是一個反模式做:片上的參數和其他對象
var args = Array.prototype.slice.call(arguments, 0);
但是我的問題是,只是這行代碼。我不確定它爲什麼有效。當我打印出來的參數對象,例如
function test(){
console.log(arguments);
}
test(1,2)
//Outputs: { '0': 1, '1': 2 }
如果我切片arguments對象
function test(){
console.log(Array.prototype.slice.call(arguments));
}
test (1,2)
//Outputs: [1,2]
我會得到的參數在數組中。 即使我追加的東西arguments對象切片仍然得到參數數組中的:
function test(){
arguments['2'] = 3;
console.log(arguments)
console.log(Array.prototype.slice.call(arguments));
}
test (1,2)
//Outputs: { '0': 1, '1': 2, '2': 3 }
//[ 1, 2 ]
如果它把一個對象作爲一個對象來slice.call
Array.prototype.slice.call({'0':1, '1':2}, 0)
我會得到一個空陣列。 任何想法爲什麼切片在參數對象上工作?
https://github.com/v8/v8/blob/master/src/js/array.js < - 我相信這是該方法實施的地方。 – djfdev