Open In App

How to Extend an Object in JavaScript ?

The extends keyword can be used to extend the objects as well as classes in JavaScript. It is usually used to create a class which is child of another class.

Syntax:



class childclass extends parentclass {...}
class parentclass extends in-built object {...}

Below example depicts how a child class uses properties of parent class using the keyword extends and by creating objects of the child class. In example 1, we see that class Profile has two attributes name and age. Now we will see that class Student acquires both attributes of class Profile using the keyword extends with an added attribute languages and then all attributes are displayed.

Example 1: In this example, we use the extends keyword.



Example 2: In this example, we will see how spread syntax can be used to extend two objects into a third object and display the containing attributes of both objects.


Article Tags :