Use the extends keyword or spread syntax to extend to extend object in JavaScript. Do extends the classes to create a class which is child of another class.
class childclass extends parentclass {
...}
class parentclass extends in-built object {
...}
JavaScript extends object
Simple example code child class uses properties of parent class using the keyword extends and by creating objects of the child class.
<!DOCTYPE html>
<html>
<body>
<script>
class Profile {
constructor(name, age) {
this.name = name;
this.age = age;
}
getName() {
return this.name;
}
getAge() {
return this.age;
}
getClass() {
return this;
}
}
class Student extends Profile {
constructor(name, age, languages) {
super(name, age);
this.lang = [...languages];
}
getDetails() {
console.log("Name : " + this.name);
console.log("Age : " + this.age);
console.log("Languages : " + this.lang);
}
}
// Creating object
var s1 = new Student("John", 25,
['Java', 'Python', 'PHP', 'JS']);
s1.getDetails();
</script>
</body>
</html>
Output:

Using spread syntax to extend two objects into one
<script>
// Creating first object
var obj1 = {
name: 'John',
age: 25
};
// Creating second object
var obj2 = {
name: 'Steve',
marks: 50
};
var object = {
...obj1,
...obj2
};
console.log(object);
</script>
Output: Object { name: “Steve”, age: 25, marks: 50 }
Use jQuery’s $.extend
var BI = BI || {};
BI = {
firstInit: function () {
console.log('I am first init');
}
}
$.extend(BI, {
init: function () {
console.log('I am init');
}
});
console.log(BI);
Do comment if you have any doubts or suggestions on this Js object topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version