JS Coding Questions Logo
JS Coding Questions
#284💼 Interview💻 Code

How do you extend classes

Advertisement

728x90

The extends keyword is used in class declarations/expressions to create a class which is a child of another class. It can be used to subclass custom classes as well as built-in objects. The syntax would be as below,

javascript
1class ChildClass extends ParentClass { ... }

Let's take an example of Square subclass from Polygon parent class,

javascript
1class Square extends Rectangle {
2  constructor(length) {
3  super(length, length);
4  this.name = "Square";
5  }
6
7  get area() {
8  return this.width * this.height;
9  }
10
11  set area(value) {
12  this.area = value;
13  }
14  }

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 28

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
284of476