有没有办法调用一个可变长度的插件函数。我将用户输入放入一个看起来像
的变量中Uinput = [5,3,2];
现在我想根据这些数字调用我的插件,这样就可以了
addon.myaddon(5,3,2);
我还想将其扩展到 n 个输入,这样如果我的用户输入变量变为
Uinput = [5,3,2,6,...,n];
然后插件将被调用
addon.myaddon(5,3,2,6,...,n);
addon.myaddon(Uinput) // will not seperate the inputs by commas are they are in the array variable, it treats the whole array as the input
这看起来很简单,但给我带来了一些麻烦。有什么建议吗?
最佳答案
Uinput = [5,3,2,...,7]; // the last number in the array is in position 'n'
addon.myaddon.apply(null, Uinput);
这相当于调用:
addon.myaddon(Uinput[0], Uinput[1], Uinput[2], ... , Uinput[n]);
使用 Math.max
的真实示例:
// Basic example
Math.max(1,6,3,8,4,7,3); // returns 8
// Example with any amount of arguments in an array
var mySet = [1,6,3,8,4,7,3];
Math.max.apply(null, mySet); // returns 8
关于javascript - 将可变长度数组传递给函数 Node JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409431/