knockout-es6 icon indicating copy to clipboard operation
knockout-es6 copied to clipboard

accessing this before calling super()

Open cl1ck opened this issue 9 years ago • 3 comments

I found some error which will work with available transpilers, but is not valid ES6: accessing this before calling super() will throw a ReferenceError as this will not yet be initialized. You should think about refactoring the property import to a utility function.


constructor(){
    this.firstName = 'Planet'; // ReferenceError
    this.lastName = 'Earth';
    super();
  }

cl1ck avatar Apr 17 '15 07:04 cl1ck

Yes, the spec has changed since I made this example. There needs to be a new method added to the base that can be called at the end of the constructor, something like this:

constructor(){
    super();
    this.firstName = 'Planet'; // ReferenceError
    this.lastName = 'Earth';
    this.knockout();
}

I wanted to wait until the ES6 extend spec was finalized before looking into this again. But if you are getting issues with transpilers that probably means they have changed to follow the new spec. What transpiler did you test it with?

mariusGundersen avatar Apr 17 '15 07:04 mariusGundersen

I meant it does work with transpilers, though it isn't valid ES6... Might be misleading to some as I wasn't aware of this myself when following your example.

cl1ck avatar Apr 17 '15 08:04 cl1ck

aha, ok. Transpilers will probably update their implementation to match the spec soon. I'll see if I can get some time to look into this later on.

mariusGundersen avatar Apr 17 '15 08:04 mariusGundersen