I would like to know if I'm doing it properly...
Having this code:
class Room {
constructor(type, size, hasWindows, equipment) {
this.type = type;
this.size = size;
this.hasWindows = hasWindows;
this.equipment = ['esterillas', ...equipment];
};
};
class PilatesRoom extends Room {
};
const room1 = new PilatesRoom('pilates', 20, true, ['balón medicinal'])
console.log(room1);
//returns: PilatesRoom {type: "pilates", size: 20, hasWindows: true, equipment: Array(2)}
I mean... I don't really need to use "constructor" and "super" to make it works perfectly, but when I check it on the internet, everybody uses it. Should I? For example:
class PilatesRoom extends Room {
constructor(type, size, hasWindows, equipment) {
super(type, size, hasWindows, equipment)
};
};
This returns the same.
I'm trying to understand! Thank you guys for your time.
You don't have to add a child class constructor if it doesn't add any logic. (In fact, static analysis and code quality tools sometimes flag it as a "useless constructor" and give a warning.)
Some programmers prefer the explicit definition of the constructor, some may carry over habits from other languages which may require it, etc. But unless the child constructor is actually doing something for the child class other than just passing the same values to the parent constructor, it's not necessary.