Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

JavaScript Prototypes and Prototypal Inheritance

Understand JavaScript's prototype-based inheritance model.

jiocoder TeamUpdated Jun 6, 20261 min readJavaScript
JavaScript Prototypes and Prototypal Inheritance

Prototype Chain#

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

Person.prototype.greet = function() {
  return `Hello, I'm ${this.name}`;
};

const person = new Person('John');
person.greet(); // "Hello, I'm John"

// Prototype chain: person -> Person.prototype -> Object.prototype -> null

Inheritance#

Code
function Student(name, school) {
  Person.call(this, name);
  this.school = school;
}

Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;

Student.prototype.study = function() {
  return `Studying at ${this.school}`;
};

Article discussion

(0)

Join the discussion — login to comment and help other developers.

Login to comment