JS Classes

The constructor method is a special method for creating and initializing an object created within a class. 

There can be only one special method with the name "constructor" in a class. Having more than one occurrence of a constructor method in a class will throw a SyntaxError error.

A constructor can use the super keyword to call the constructor of a parent class.

If you do not specify a constructor method, a default constructor is used.

class Square {
    constructor() {
        this.name = "Square";
    }
}

 

The extends keyword is used in class decorations or class expressions to create a class which is a child of another class.

class ChildClass extends ParentClass { ... }

 

Static method calls are made directly on the class and are not callable on instances of the class. Static methods are often used to create utility functions.

static methodName() { ... } class StaticMethodCall { static staticMethod() { return 'Static method has been called'; } static anotherStaticMethod() { return this.staticMethod() + ' from another static method'; } }