JavaScript Prototypes and Inheritance
Prototype Inheritance
JavaScript's object-oriented inheritance mechanism.
Prototype Example
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return "Hi, I'm " + this.name;
};
function Student(name, grade) {
Person.call(this, name);
this.grade = grade;
}
Student.prototype = Object.create(Person.prototype);
let student = new Student("John", "A");
console.log(student.greet()); // "Hi, I'm John"Key Points
__proto__, prototypechain.Object.create()for prototypal inheritance.- Class syntax (ES6) syntactic sugar over prototypes.
instanceofchecks prototype chain.