What is prototypal inheritance in JavaScript?
Objects inherit from other objects via the prototype chain. When you access a property, JS checks the object, then its prototype, then the prototype's prototype, up to null. This is different from class-based inheritance in Java/C++.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Prototypes and Prototypal Inheritance in JavaScript
Use Object.create(proto): const dog = Object.create(animal). dog inherits all properties from animal. You can also add own properties: Object.create(animal, { bark: { value: () => 'barking' } }).
__proto__ is the actual prototype of an instance (on every object). prototype is on constructor functions and is the object that instances will inherit from. new Person() creates an object whose __proto__ is Person.prototype.
ES6 classes are syntactic sugar over prototypes. class Person { greet() {} } puts greet on Person.prototype. extends creates the prototype chain. Under the hood, it is the same prototypal inheritance.
Still have questions?
Browse all our FAQs or reach out to our support team
