1

是否可以在不使用的情况下将多个(未知)参数传递给函数Array

看看这个示例代码。

var test = function( /* Arguments */ ) { // Number 3
    something( /* All Arguments Here */ );
};


var something = function( first, last, age ) { // Number 2
    alert( first + last + age );
};


test('John', 'Smith', 50); // Number 1 

那么问题...

是否可以在不影响使用方式的情况下将参数从Number 1TO Number 2VIA传递。Number 3IE。没有Array.

这可能与此有关,OCD但使用数组会很讨厌。

我尝试过什么吗?,我想不出什么我可以尝试的......我可以尝试什么?我已经搜索过了......

4

2 回答 2

4
var test = function() { // 数字 3
    something.apply(null, arguments);
};


var something = function(first, last, age) { // 数字 2
    警报(第一个 + 最后一个 + 年龄);
};


测试('约翰','史密斯',50);// 1号
于 2013-07-29T09:31:53.280 回答
2

感谢 Blade-something,我找到了答案

你会用Array.prototype.slice.call(arguments)

var test = function( /* Arguments */ ) {
    something.apply(null, Array.prototype.slice.call(arguments));
};


var something = function( first, last, age ) {
    alert( first + last + age );
};


test('John', 'Smith', 50);

演示

如果您不想保留其余参数并且不想保留第一个参数供内部使用,则此示例非常有用

var test = function( name ) {
    // Do something with name
    something.apply(null, Array.prototype.slice.call(arguments, 1));
};
于 2013-07-29T09:27:19.907 回答