The guide that will make you understand prototypal inheritance in JavaScript: prototype object, inherited propertes, chain of prototypes.
You don’t know JavaScript until you know prototypal inheritance. Prototypes greatly influence how objects work in JavaScript.
The prototypal inheritance is often asked during coding interviews since this knowledge is an indicator of how you know JavaScript.
This guide will help you easily understand prototypal inheritance in JavaScript.
JavaScript has only primitives types, null
, undefined
and objects. A big world of objects. In JavaScript, contrary to languages like Java or PHP, there’s no concept of class that serves as a template to create objects.
An object is a composable structure and consists of multiple properties: key and value pairs.
For example, the following object cat
contains 2 properties:
const cat = { sound: 'Meow!', legs: 4 };
Since I’d like to reuse legs
property in other objects, let’s extract legs
property into a specialized object pet
:
const pet = { legs: 4 };
const cat = { sound: 'Meow!' };
That looks better.
But I still want to have legs
property on cat
. How can you connect cat
with pet
?
Inheritance can help you!
#javascript #programming #developer #web-development