Mid JavaScript

How does inheritance work in JavaScript?

  • Objects inherit from other objects via Object.create(), constructor functions,

or classes (ES6).

  • Methods can be shared via prototypes.

Example using constructor:

function Person(name) { this.name = name; }

Person.prototype.greet = function() { console.log(`Hello

${this.name}`); };

const p = new Person("Sandeep");

p.greet(); // Hello Sandeep

More from JavaScript Tutorial

All questions for this course