Prototypal inheritance is one of the topics in programming every developer should know. This tutorial will help you learn what prototypal inheritance is, how it works and how to use it. You will learn about [[Prototype]]__proto__, how to change prototypes and much much more.

Creating, inheriting and re-usability

Knowing how to create objects is useful. It can help you do more things, often in better way. However, creating objects from scratch may not always be the best thing to do. The problem is that this practice can lead to repeated code. What you can do instead is create a base object.

This base object will contain universal properties and methods you may want in other objects. Later, let’s say you want to create an object that will use any of these properties or methods. You don’t have to write all those properties and methods from scratch. Instead, you can let that new object inherit from the base object.

When you do this, that new object will be able to use any property and method that exists in the base object. This is not everything you can do, yet. You can also add additional properties and methods only to that new object. After this, the base object will still be the same.

That new object, however, will not only be able to use anything from the base object. It will also be able to use anything new you’ve just added. This degree of re-usability can help you make your code much shorter, clearer and cleaner. This is how prototypal inheritance can help you.

The [[Prototype]] property

The fundamental part of prototypal inheritance is the [[Prototype]] property. This is a special hidden property that exists on every object in JavaScript. The value of this property is always either null or name of another object. When the value of [[Prototype]] is null it means that the object doesn’t inherit from any other object.

When the value is a name of another object it means that the object’s prototype references another object. Put simply, that object inherits from another object, whose name is specified in [[Prototype]]. When this happens, the inheriting object can use any property and method from the object it inherits from.

#javascript #prototypal inheritance

Objects, [[Prototype]] and Prototypal Inheritance in JavaScript
1.10 GEEK