Javascript Class

Javascript Class:

Provides prototype-based inheritance

Define class:

1. Class declarations:

Syntax:


class Rectangle {

constructor(height, width) {

this.height = height;
this.width = width;

}

}

2. Class expressions

Syntax:

var Rectangle = class {

constructor(height, width) {

this.height = height;
this.width = width;

}

};

Example:

class Rectangle {


constructor(height, width) {

this.height = height;
this.width = width;

}
// Getter
get area() {

return this.calcArea();

}
// Method
calcArea() {

return this.height * this.width;

}

}

const square = new Rectangle(10, 10);

console.log(square.area); // 100