Skip to main content

classes

Class tips

method chaining

You can chain methods together on your class by returning this inside every method.

class DateClass {
public chain1() {
console.log("chain1");
return this;
}

public chain2() {
console.log("chain2");
return this;
}

public chain3() {
console.log("chain3");
return this;
}
}

const date = new DateClass();
date.chain1().chain2().chain3();

Static inheritance

Classes inherit all static methods and properties from parent classes. You can take further advantage of this by using super for overriding static properties and methods from the parent.

class Parent {
static attributes = ["hello"]
}

class Child extends Parent {
static attributes = [...super.attributes, "dog"]
}

console.log(Child.attributes) // ["hello", "dog"]