If using ES6 `extends`, call `super()` before accessing `this`
I am working on rewriting some code that used an ES5 "Class" helper, to use actual ES6 classes.
I soon stumbled upon a weird error in which apparently valid code would be throwing an |this| used uninitialized in A class constructor error:
class A extends B {
constructor() {
this.someVariable = 'some value'; // fails
}
}
I was absolutely baffled as to why this was happening... until I found the answer in a stackoverflow post: I had to call super() before accessing this.
With that, the following works perfectly:
class A extends B {
constructor() {
super(); // ☜☜☜ ❗️❗️❗️
this.someVariable = 'some value'; // works!
}
}
Edit: filed a bug in Firefox to at least get a better error message!