JavaScript Classes
JavaScript classes were introduced in ECMAScript 2015(ES6). They are primarily syntactical sugar over JavaScript’s existing prototype based inheritance.
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
area() {
return (this.width * this.height);
}
}
The constructor
method is a special method for creating and initializing an object created with a class.
A constructor
can use the super
keyword to call the constructor of the super class.
Sub classes can be created with extends
keyword.
class Square extends Rectangle {
constructor(width, height) {
super(width, height);
}
area() {
let result = super.area();
console.log("Area of square is ", result);
}
}
References
Categories :
JavaScript
ES6