Created
September 8, 2020 16:38
-
-
Save karol-majewski/94b0ada5501eed0796bcfabae9177446 to your computer and use it in GitHub Desktop.
Property binding order in JavaScript classes (what TypeScript will not save you from)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo { | |
options = { | |
ns: this.namespaces | |
}; | |
constructor(namespaces = []) { | |
this.namespaces = namespaces; | |
} | |
} | |
console.log( | |
new Foo().namespaces, // [] | |
new Foo().options.ns // undefined | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you do this in TypeScript:
The generated JavaScript will look like this:
Which makes it work.
On the contrary, Babel does not pull property assignment into the constructor.