Facebook Pixel

How do ES6 classes relate to prototypes?

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.

Verify This Answer

Cross-check this information using these trusted sources:

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?

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
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.