I've read a lot of amazing articles and discussions (like this one) about prototype property of an object but there is a big question here. When we observe the behavior of "prototype" property we realize that it is actually another object. We can see it has its own proto and constructor properties just like an object.
var myobj= function(){};
myobj.prototype.constructor== myobj --> true
So these are the questions :
- Is "prototype" itself an object?
- What is the relationship between prototype and the object it's related to (in this example myobj)?
- Why myobj.prototype.__proto__ is by default the "Object{ }" but its constructor is myobj?
- We know : "
__proto__
is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build__proto__
when you create an object with new". But we can see that this is not just what prototype does. I think : Prototype acts like a container object of all the shared methods and properties of an object! Then, when an instance of an object is created using new, its internal [[Prototype]] points to the prototype where the shared behaviors and properties of a class is exposed to the instances! Is this true?
In the end : It seems when we instantiate a class, the constructor of the instance is set to the constructor of the prototype of that object. Can this conclusion true?
var b= new myobj();
b.constructor== myobj.prototype.constructor --> true
b.constructor == myobj --> true
myobj.prototype={};
b.constructor == myobj --> false
b.constructor== Object --> true
myobj.prototype.constructor== Object --> true