Saturday 22 December 2018

What is __proto__ in JavaScript?

__proto__ is a property, it points to the object which was used as prototype when the object was instantiated.

Let me explain with an example.

HelloWorld.js
class A{
  
}

class B extends A{
  
}

class C extends B{
  
}

var obj = new C();

var proto = obj.__proto__;

while(proto != null){
  console.log(proto.constructor);
  proto = proto.__proto__;
}

As you see above example, Class C is inheriting the properties from Class B, Class B inherit from class A.

For the object ‘obj’, class C is used as prototype,
for the class C, class B is used as prototype,
for the class B, class A is used as prototype.
For the class A, Object class (it is the super class for all the classes) is used as prototype.

When you ran HelloWorld.js, you can see below messages in console.



Previous                                                 Next                                                 Home

No comments:

Post a Comment