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.
Asked
Active
Viewed 1.1k times
26

Turner Hayes
- 1,844
- 4
- 24
- 38
-
The spread operator is a better apply. – Oriol Jul 18 '16 at 22:58
-
`super.constructor.apply(this, arguments)` would work if `this` was already initialised – Bergi Jul 18 '16 at 23:14
1 Answers
51
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:

zerkms
- 249,484
- 69
- 436
- 539
-
3If you are using this in a browser, just be aware of spread operator support as support is fairly new and for some reason support for the spread operator came later than other ES6 features such as `class`. For example, I don't think there's any IE support. You could, of course, also use a transpiler. – jfriend00 Jul 18 '16 at 23:00
-
2@jfriend00 IE does not support almost anything from ES2015, so, who cares :-) – zerkms Jul 18 '16 at 23:00
-
3It's easy to find versions of Chrome in use that aren't the latest and support `class`, but don't support the spread operator. I'm just advising the OP to make sure they have the browser support they need. Don't get so defensive. This is a nice answer if your environment supports it. – jfriend00 Jul 18 '16 at 23:02
-
Ah, I wasn't aware of the use of the spread operator as an apply, that's very handy. Thanks! – Turner Hayes Jul 18 '16 at 23:38