Since JavaScript can be one huge mess if not managed properly, we're collecting here some conventions we'll be using in our project to favor consistency, performance and elegance.
As of now, we just import all of the required js files in the right order in the HTML document calling them. In the future we may turn to ES6 modules.
class Musician {
constructor (name, earnings) {
// Public fields
this.name = name;
// Private fields
this._earnings = earnings;
}
// Public methods
printDiscography () {
console.log("Many cool records");
}
// Private methods
_printResidence () {
console.log("You should not call this from outside the class");
}
}
Notes:
- The syntax for private fields and methods will be easily updated to the WIP private fields/methods of JS by just replacing "_" with "#".
- This way of declaring methods only creates one function object in the prototype, which is referenced by all instances. This is good compared to techniques that end up creating a new one per instance.
let me = new Musician("Jotaro", 0);
inst.printDiscography();
class Drummer extends Musician {
constructor(name, earnings, doubleBass) {
// Fields of the parent class
super(name, earnings);
// Specific fields
this.doubleBass = doubleBass;
}
}
let moreSpecificObject = new Drummer ("Mike", 1000, true);
if (moreSpecificObject.doubleBass)
moreSpecificObject.printDiscography();