我想调用一个使用数组作为参数的函数:
const x = ['p0', 'p1', 'p2']; call_me(x[0], x[1], x[2]); // I don't like it function call_me (param0, param1, param2 ) { // ... }
有没有更好的方法来传递xinto的内容call_me()?
x
call_me()
const args = ['p0', 'p1', 'p2']; call_me.apply(this, args);
请参阅 MDN 文档以获取Function.prototype.apply().
Function.prototype.apply()
如果环境支持 ECMAScript 6,则可以使用扩展参数:
call_me(...args);