Javascript 使用参数数组调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1881905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call function with array of arguments
提问by David Hellsing
Can I call a function with array of arguments in a convenient way in JavaScript?
我可以在 JavaScript 中以方便的方式调用带有参数数组的函数吗?
Example:
例子:
var fn = function() {
console.log(arguments);
}
var args = [1,2,3];
fn(args);
I need argumentsto be [1,2,3], just like my array.
我需要arguments成为[1,2,3],就像我的数组一样。
回答by CMS
Since the introduction of ES6, you can sue the spread syntaxin the function call:
自从引入 ES6 后,你可以在函数调用中起诉spread 语法:
const args = [1,2,3];
fn(...args);
function fn() {
console.log(arguments);
}
Before ES6, you needed to use apply.
在 ES6 之前,你需要使用apply.
var args = [1,2,3];
fn.apply(null, args);
function fn() {
console.log(arguments);
}
Both will produce the equivalent function call:
两者都会产生等效的函数调用:
fn(1,2,3);
Notice that I used nullas the first argument of the applyexample, which will set the thiskeyword to the global object (window) inside fnor undefinedunder strict mode.
请注意,我用作示例null的第一个参数apply,它将在严格模式内或在严格模式下将this关键字设置为全局对象 ( window) 。fnundefined
Also, you should know that the argumentsobject is not an array, it's an array-like object, that contains numeric indexes corresponding to the arguments that were used to call your function, a lengthproperty that gives you the number of arguments used.
此外,您应该知道该arguments对象不是数组,而是类似数组的对象,其中包含与用于调用函数的参数相对应的数字索引,该length属性为您提供所使用的参数数量。
In ES6, if you want to access a variable number of arguments as an array, you can also use the rest syntaxin the function parameter list:
在 ES6 中,如果你想以数组的形式访问可变数量的参数,你也可以使用函数参数列表中的其余语法:
function fn(...args) {
args.forEach(arg => console.log(arg))
}
fn(1,2,3)
Before ES6, if you wanted to make an array from your argumentsobject, you commonly used the Array.prototype.slicemethod.
在 ES6 之前,如果你想从你的arguments对象中创建一个数组,你通常使用该Array.prototype.slice方法。
function fn() {
var args = Array.prototype.slice.call(arguments);
console.log(args);
}
fn(1,2,3);
Edit:In response to your comment, yes, you could use the shiftmethod and set its returned value as the context (the thiskeyword) on your function:
编辑:响应您的评论,是的,您可以使用该shift方法并将其返回值设置为this函数的上下文(关键字):
fn.apply(args.shift(), args);
But remember that shiftwill remove the first element from the original array, and your function will be called without that first argument.
但请记住,这shift将从原始数组中删除第一个元素,并且您的函数将在没有第一个参数的情况下被调用。
If you still need to call your function with all your other arguments, you can:
如果您仍然需要使用所有其他参数调用您的函数,您可以:
fn.apply(args[0], args);
And if you don't want to change the context, you could extract the first argument inside your function:
如果您不想更改上下文,则可以提取函数中的第一个参数:
function fn(firstArg, ...args) {
console.log(args, firstArg);
}
fn(1, 2, 3, 4)
In ES5, that would be a little more verbose.
在 ES5 中,这会更冗长一些。
function fn() {
var args = Array.prototype.slice.call(arguments),
firstArg = args.shift();
console.log(args, firstArg);
}
fn(1, 2, 3, 4);
回答by Micha? Per?akowski
In ECMAScript 6, you can use spread syntax (...)for that purpose. It's way simpler and easier to understand than Function.prototype.apply().
在 ECMAScript 6 中,您可以为此使用扩展语法 ( ...)。它比Function.prototype.apply().
Code example:
代码示例:
const fn = function() {
console.log(arguments);
}
const args = [1,2,3];
fn(...args);

