Classes are in fact “special functions”, and just as you can define function expressions and function declarations, the class syntax has two components: class expressions and class declarations.
Class declarations
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
const R1 = new Rectangle();
// unnamed
let Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle"
// named
let Rectangle = class Rectangle2 {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle2"
Static Properties
Static properties are a group of class features that are defined on the class itself, rather than on individual instances of the class. These features include:
- Static methods
- Static fields
- Static getters and setters
Super
In JavaScript, super
refers to the parent class constructor. (In our example, it points to the React.Component
implementation.)
Importantly, you can’t use this
in a constructor until after you’ve called the parent constructor. JavaScript won’t let you: