Classes
ES6 Classes
Visibility
Meanwhile class fields get supported, visibility rules are just based on a common naming convention where properties and methods get prefixed with the underscore character _
if they are private.
Properties
Avoid using properties.
Properties can become handy in some extreme cases like the integration of some legacy library, mock, implementing a runtime type checking decorator or something fancy like that etc...
Otherwise, properties will only introduce ambiguity.
Who would imagine that this code might trigger a runtime exception?
Or even worse, things might end up like this:
Inheritance
This is inheritance:
Now, avoid it...
... and prefer composition!
Best practices
Meanwhile we get the class fields in JavaScript, it is recommended to initialize all the attributes in the constructor. Otherwise, it's hard to figure out which attributes are available on a class. In addition to this, the available attributes will depend on the methods that have been called.
Last updated