ES6 class super() with variadic arguments

点点圈 提交于 2019-11-30 13:10:24

问题


In ES6, is there a way to call a parent constructor passing through variadic arguments, a la foo.apply(this, arguments)? I've looked for an answer, and the only instances I see are either calling super() (no arguments) or calling super(x, y) (with specific arguments). super.apply(this, arguments) doesn't appear to work.


回答1:


The pattern I find convenient and follow is

constructor(...args) {
    super(...args);
}

In case you have and use named arguments you could do this instead:

constructor(a, b, c) {
    super(...arguments);
}

References:

  • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator
  • https://kangax.github.io/compat-table/es6/


来源:https://stackoverflow.com/questions/38447168/es6-class-super-with-variadic-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!