JavaScript - How does Inheritance work in Javascript

In JavaScript, you can use Object Oriented Design concept and one of them is inheritance. In JavaScript, you can create child object from parent object and can override some properties if you want. Here is the example.

We have course Object which has properties name, duration and location with their respective values.

course = {name:'CKD', duration:'3 months', location:'india'}

Now, below we are creating another object with course object but would override duration property.

course1=Object.create(course,{duration:{value:'1 year'}})

Here is the output in which you can see that the properties of course1 are same as course but the value of duration is "1 year" because it was overridden.

javascript inheritance

Tags