Facebook Pixel
Prototypes and Prototypal Inheritance in JavaScript

How do ES6 classes relate to prototypes?

The answer

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.

More FAQs in Prototypes and 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++.

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.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.