JavaScript objects have a hidden property [[Prototype]]
& it is called prototype.
There are many ways to set [[Prototype]], one of them is __proto__
:
let bird = { fly: true};let eagle = { eats: true};
// sets eagle.[[Prototype]] = birdeagle.__proto__ = bird;
Now when we log the property eagle.fly
it will log true.
console.log(eagle.fly) // trueconsole.log(eagle.eats) // true
The references can be made to multiple levels.
It has some limitations:
- References can’t be circular. JavaScript will throw an error if we try to assign
__proto__
in a circle. - The value of
__proto__
can be only object of null. No other value is allowed.
Note: this blog is under progress