[英]Simplify passing same arguments two two different functions
I'm calling two functions to which I pass the identical arguments.我正在调用两个函数,我将相同的 arguments 传递给它们。
foo1('p1','p2','p3','p4','p5');
//...
foo2('p1','p2','p3','p4','p5');
Can I do something like the following, to declare the arguments once and then pass it to both functions?我可以执行以下操作来声明一次 arguments 然后将其传递给两个函数吗?
var params = 'p1','p2','p3','p4','p5'; // what's the syntax here?
foo1(params);
foo2(params);
Yes, you can: Put them in an array and use ...
(in modern environments) or apply
(in older environments):是的,您可以: 将它们放在一个数组中并使用...
(在现代环境中)或apply
(在旧环境中):
In modern environments:在现代环境中:
const params = ['p1','p2','p3','p4','p5'];
// −−−−−−−−−−−−^−−−−−−−−−−−−−−−−−−−−−−−−^
foo1(...params);
// −−^^^
foo2(...params);
// −−^^^
In older environments:在旧环境中:
var params = ['p1','p2','p3','p4','p5'];
// −−−−−−−−−−^−−−−−−−−−−−−−−−−−−−−−−−−^
foo1.apply(null, params);
// −^^^^^^^^^^^^
foo2.apply(null, params);
// −^^^^^^^^^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.