javascript - 如何在 Javascript 中用单个变量编写多个参数?

标签 javascript function arguments

我为许多功能动态使用单个结构,如下所示:

Parent[funcName](data, function(err) {
   // further operations
});

函数中的变量“data”根据函数要求有0、1、2或3。当在“data”中传递单个参数时它工作正常,但如果超过 1,它会显示错误 - “需要更多参数”。

那么如何使用单个变量传递多个参数呢?有什么解决办法吗?

最佳答案

You can use the spread operator

Parent[funcName](...data, function(err) {
   // further operations
});

其中data是您传递的所有参数的数组。

关于javascript - 如何在 Javascript 中用单个变量编写多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49149986/

相关文章:

javascript - 嵌套在面板内的显示模式

javascript - 从相对 URL 获取绝对 URL。 (IE6问题)

javascript - 从 1970 年之前的日期数组中获取最高日期

c - 在函数内部使用指针时出错

c++ - STACK.peek 函数,遇到一些问题,C++

C 预处理器流控制取决于函数参数

Python 函数基于参数进行排序和操作

javascript - Jasmine 单元测试: Can't trigger change event in custom angular dropdown component

javascript - 如何在函数调用中指定(默认)参数而不使用 undefined

javascript - 将 jQuery (ajax) 与 TypeScript 结合使用