Inheriting from a Class: extends keyword and Prototype Chaining
Learn about the actions of the extends keyword and discuss how the prototype chain is changed and get shared.
We'll cover the following
extends
implies prototypal inheritance
Even though JavaScript uses the extends
keyword, which is popular for inheritance in Java, it’s important to remember that the inheritance is prototype-based and not class-based. Let’s verify this key design for inheritance in JavaScript with an example.
Example
We’ll continue with the Person
and ReputablePerson
classes we used in the previous lesson. We can obtain the prototype of an object using the Reflect.getPrototypeOf()
method. Since prototypes form a chain, we can repeatedly or recursively use this method to walk the inheritance hierarchy—that is, the prototype chain.
Let’s write a function that will print a given object, and then print the object’s prototype chain.
Get hands-on with 1400+ tech skills courses.