Why do we use a temporary box in prototype inheritance?

https://stackoverflow.com/questions/47843315/why-do-we-use-a-temporary-box-in-prototype-inheritance

The function below is one that Derek Banas on youtube on his OO Javascript tutorial uses.

You have a function A that you want as a child. You have function B that you want as a parent.
You want to connect them using a function called extend.

The implementation is:

but why is this so?

Prototype objects are single literal objects. We need to create a Temp function that points to the Parent prototype object first, then spawn a instance, which acts as a single literal object.

This single instance will act as the Child’s Prototype Object, which is connected to Parent’s Prototype object.

1) Creating a custom Child Prototype object.

It should be done by like so:

which will give you an empty object with its __proto__ pointing to Foo’s prototype object. However, since we’re gunna do it by hand, we’ll do it step by step.

Setting up Child Prototype Object

First, we create the function Parent. By default, it will have a literal object called Parent Prototype. This Parent Prototype will have __proto__ pointing to default Object Prototype.

– Parent will have a ‘prototype’ reference to Parent Prototype
– Parent Prototype will have a ‘constructor’ reference to Parent

In order to create a Child Prototype Object, we first must create a function Temp to estabblish a Temp setup. The Temp will have a prototype reference to a Temp Prototype object. The Temp Prototype will have a constructor reference back to the temp. And by default, the Temp Prototype will have a __proto__ to the default Object Prototype, as shown in the diagram.

Then, with this setup, if we create an instance of Temp, that instance of Temp, will have its __proto__ pointing to Parent Prototype. We then call this new Temp object “Child Prototype Object”

From here, when we instantiate a child object, it will have its __proto__ pointing to Child Prototype Object. We can access any functions declared there. If it doesn’t exist, it will go up the hierarchy tree via __proto__, which is Parent Prototype. If it does not find it there, it goes up the next __proto__, which is the Object Prototype. And if its not there, then what we’re looking for does not exist.