Because there are no classes you get an object by simply creating one. For example, the following creates an object that has a single method.
This allows me to introduce myself:
Of course this is nice and all but not of too much help. If I want to create yet another person who can introduce herself I have to duplicate the introduce function. Like class-based programming languages group behavior in classes, we want to group the behavior of a person in an object prototype.
The prototype would look something like this:
__proto__. [This is not recommended and I’ll get to that in a second.]
Like I said, this is not recommended. I included this example because I want to show that there’s not much magic going on, it’s just all about the objects. The reason for this not being recommended is that
__proto__ had the value of
Object before manually changing it to
[Normally I would pass in
name as a constructor argument on
Person as a function now, something else changed here. Something important. The
introduce function is no longer an attribute of
Person but instead of its
The way to copy from objects is by using keyword
new on a function. This implies that you can only copy from objects that are actually a constructor and thus objects that have
Function in their prototype-chain (and most of the time are direct descendants of it). Because
In the beginning I also told you a lie. It should be clear by now that the internal prototype of the object was not
Object but rather
Object.prototype. This is also where the prototype-chain ends.