如何在 javascript 中将列表转换为多个参数
How to convert a list into multiple arguments in javascript
例如,
var listToBecomeArguments = ["foo","bla"];
myFunc(listToBecomeArguments);
当被调用时会做这样的事情:
function myFunc(arg1, arg2) {
// arg1 is "foo", arg2 is "bla"
}
为了你的功能
function myFunc(arg1, arg2) {...}
像这样使用 .apply() method:
myFunc.apply(null, listToBecomeArguments);
它会做同样的事情:
myFunc(listToBecomeArguments[0], listToBecomeArguments[1],...);
你可以使用局部变量 arguments
来做你想做的事情。
function myFunc() {
console.log(arguments);
}
myFunc("param1", "param2");
// Output:
// > ["param1","param2"]
引用上面的MDN文章,arguments
是
an Array-like object corresponding to the arguments passed to a function.
例如,
var listToBecomeArguments = ["foo","bla"];
myFunc(listToBecomeArguments);
当被调用时会做这样的事情:
function myFunc(arg1, arg2) {
// arg1 is "foo", arg2 is "bla"
}
为了你的功能
function myFunc(arg1, arg2) {...}
像这样使用 .apply() method:
myFunc.apply(null, listToBecomeArguments);
它会做同样的事情:
myFunc(listToBecomeArguments[0], listToBecomeArguments[1],...);
你可以使用局部变量 arguments
来做你想做的事情。
function myFunc() {
console.log(arguments);
}
myFunc("param1", "param2");
// Output:
// > ["param1","param2"]
引用上面的MDN文章,arguments
是
an Array-like object corresponding to the arguments passed to a function.