Javascript 我可以用'arguments'应用'fn.apply'吗?还是';我必须先将'arguments'转换为数组吗?

Javascript 我可以用'arguments'应用'fn.apply'吗?还是';我必须先将'arguments'转换为数组吗?,javascript,arrays,arguments,Javascript,Arrays,Arguments,我正在学习。apply和arguments我有一个问题-我可以fn.使用arguments应用,还是必须先将arguments转换为数组 我创建的这个代码示例表明,我可以使用参数应用: function aaa () { bbb.apply(undefined, arguments) } function bbb () { console.dir(arguments) } aaa(1,2,3,4) // calls bbb and bbb's arguments are 1,

我正在学习
。apply
arguments
我有一个问题-我可以
fn.使用
arguments
应用
,还是必须先将
arguments
转换为数组

我创建的这个代码示例表明,我可以使用
参数
应用

function aaa () {
    bbb.apply(undefined, arguments)
}

function bbb () {
    console.dir(arguments)
}

aaa(1,2,3,4) // calls bbb and bbb's arguments are 1, 2, 3, 4
我还可以将参数转换为数组,它也可以工作:

function aaa () {
    var args = Array.prototype.slice.call(arguments);
    bbb.apply(undefined, args)
}

function bbb () {
    console.dir(arguments)
}

aaa(1,2,3,4) // calls bbb and bbb's arguments are 1, 2, 3, 4

我应该使用一个还是另一个,有什么区别吗?

您不必将
参数
转换为数组:


但是,有理由直接传递
参数
,而不是在传递给
apply
之前将其转换为数组。例如,在某些浏览器中,直接传递
参数
是优化的(例如,我知道)。

您不必将
参数
转换为数组:


但是,有理由直接传递
参数
,而不是在传递给
apply
之前将其转换为数组。例如,在某些浏览器中,直接传递
参数
是优化的(例如,我知道)。

不需要将其转换为数组,您可以直接使用
参数
不需要将其转换为数组,您可以直接使用
参数
var args = {0: 1, 1: 2, 2: 3, length: 3}
// Note that this is an object, not an array

function test() { console.log(arguments); }

test.apply(undefined, args);
// Logs [1, 2, 3]