Each JS object has a property called
null as its prototype, meaning that the prototype chain is over.
[[Prototype]] property is accessed using the accessors
Object.setPrototypeOf(). It can also be accessed via the property
__proto__, which is non-standard but implemented by many browsers. For the sake of simplicity, we’re going to use
__proto__ from now on in this article, as shown below:
Note that the code above is merely a simplified version of the following:
__proto__), and this is used when the function is called with the
new keyword in order to set the prototype of the object being created.
In fact, if we check the value of
Object.prototype, we get the same result as the
As we’ve seen before,
Person has a property called
prototype. So we are going to create a function called
greetings inside of it:
Now we’ll create an object using our constructor function:
If we check
johnDoe.__proto__, we will see our
greetings function there. When we call
johnDoe.greetings(), the interpreter will check johnDoe’s properties. Since the function is not defined there, it will check its prototype (referenced by the
__proto__ function), and then we’ll get the expected result:
Now, let's find out what happens if we create our object and then change our constructor function after that, like this:
What do you think will be the output? … If you said “Hello! =),” you are correct! This is because, as we said before, the
__proto__ property is a reference to the object’s prototype and not a copy of it. You can also check that
johnDoe.__proto__ === Person.prototype is
true, meaning that both properties point to the same place.
Rafael Bicalho is a UI Engineer at Avenue Code. He is a fan of technology, coffee and programming.