It seems like there is a difference here…
Let’s say we have function MyConstructor() {}
MyConstructor’s [[Prototype]] is Function.prototype, not MyConstructor.prototype.
In other (non-standard/”console.log-able”) words:
MyConstructor.__ proto__ is not MyConstructor’s MyConstructor.prototype
TRY THIS:
function MyConstructor() {};
(MyConstructor.__proto__ === MyConstructor.prototype); //false?! why?
Why is this so? Can someone explain it to me the difference?
Think of it like this.
MyConstructoris a function object, so it was created byFunction; therefore its[[Prototype]](or__proto__) is identical toFunction.prototype.In the same way,
var myObj = new MyConstructor()creates an objectmyObjwith a[[Prototype]]identical toMyConstructor.prototype.To put it another way, functions have a
prototypeproperty, and when you invoke functions withnew, they will construct an object having a[[Prototype]]identical to the constructor function’sprototypeproperty… however a function’sprototypeproperty is not the same thing as its[[Prototype]](or__proto__) property, because a function follows the same rules as other objects and gets its internal[[Prototype]]property from the function that constructed it (which is alwaysFunction, incidentally).To explain further,
[[Prototype]]andprototypehave entirely different purposes.[[Prototype]]is used when resolving an object’s properties. If an object doesn’t have a property, its[[Prototype]]is checked, and then that object’s[[Prototype]], and so on, until either a property is found or you hit the end of the prototype chain.In contrast,
prototypeis the mechanism by which you assign[[Prototype]]properties to objects, since you can’t access them directly other than with the non-standard__proto__property.Since functions are objects, they have both a
[[Prototype]]internal property, used to resolve properties as with normal objects, and aprototypeproperty, which is assigned as the[[Prototype]]of new objects constructed by the function.