ES6 class super() with variadic arguments

后端 未结 1 1505
情书的邮戳
情书的邮戳 2020-12-29 23:36

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 onl

相关标签:
1条回答
  • 2020-12-30 00:31

    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/
    0 讨论(0)
提交回复
热议问题