Classes
Constructor
A constructor's purpose is to allocate you an object and then return immediately.
Class-related Keywords
super
The super keyword is used to access and call functions on an object's parent.
- ex. If we have
Dogextend fromMammal, then callingsuper.getinDogwill call thegetmethod of theMammalclass.
spec: When used in a constructor, calling super calls the constructor method of the parent.
- the
superkeyword appears alone and must be used before thethiskeyword is used.
Note that a rectangle is more generic than a square. Therefore, we can create a Square class that extends a Rectangle class, and simplify the interface of the Square.
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
class Square extends Rectangle {
constructor(length) {
super(length)
}
}
We can call super on methods that have the static modifier.