Prototype Chain
In this article, we will talk about prototype chain. Let’s start with what is a prototype? A prototype is from where objects inherit properties and methods.
You might be asking why is a prototype important?
Prototypes are a convenient way to define properties that will be accessible to other objects.
Allows to easily define methods for instances of an object; they get stored in memory once but every instance can access it.
They are used to create object-oriented code.
Every object can have a prototype, and an object’s prototype can have another prototype, and that object’s prototype can have another one...and so forth. This “queue” is referred to as a prototype chain. Let’s look at some code…
Important: Every function gets a prototype object every time it is created. In this case we used a function as a constructor so the constructed object’s prototype (orangeTree) is set to the function’s prototype (Tree). Take a look at the following:
Now let’s add a method to our Tree prototype, like this:
What happens when we override our prototype and replace it with something else?
In summary, prototypes are a convenient way to define properties and functionality that will be easily accessible to other objects. Their main use is to produce an object-oriented code. Each prototype has a private property that holds the chain/link to another object, thus creating a prototype chain.