Javascript functions override prototype

I found some interesting examples on SO. Among them was a link to an article. He said that:

Function.__proto__ points to Function.prototype . This leads to:

  Function.constructor === Function 

That is: a function is its own constructor!

 Object instanceof Object == true. 

This is because:

 Object.__proto__.__proto__.constructor == Object 

Note that unlike Object instanceof Object , Foo instanceof Foo == false . This is because: Foo does not exist as a constructor for its own prototype chain.

From the Mozilla developer network, I found that both prototypes and constructor functions can be easily overridden. And since instanceof just checks constructor.prototype in the prototype chain, I really don't understand why my code still returns false .

 function Foo() { } ; Foo.prototype = Foo Foo.constructor = Foo Foo instanceof Foo // still false 

The prototypes also have a few comments. Did I understand correctly that this prototype is a separate auxiliary object? And this object is like a pointer to another object - usually an Object .

+4
source share
2 answers

obj instanceof does not look for constructor in obj.prototype , but in the property of the internal object __proto__ .

Each object in javascript has an internal property __proto__ , which refers to the prototype of the object. When an object is created using the new operator, the prototype constructor property is assigned to this internal property of the __proto__ object.

That way, when you say Foo instanceof Foo , the javascript virtual machine will look for "Foo" in Foo.__proto__ . Since Foo is a function, Foo.__proto__ is Function.prototype (where Function is the constructor of functions).

Since you cannot change the internal property of an __proto__ object, Foo can never be an instance of Foo.


About your little question: in javascript, everything is an object. This includes prototypes and . In fact, ECMAScript 5 added the Object.create ( MDN ) function, which takes an object as its first parameter and, in turn, creates a new object using the first object, which is its internal prototype.

+4
source

It is important to remember that the object, the actual, internal prototype, which is part of the prototype chain, is not the same as the property of the prototype object. The internal prototype of the object is set by the property of the prototype constructor.

In the article, I found it most understandable when I found out that this is actually this one .

In other words, the only point in changing the prototype property of an object is that this object is a function that should be used as a constructor. In addition, there is nothing magical about it, it's just a property. Changing prototype does not change __proto__ and does not affect the prototype chain of the object.

So, going through the code:

 function Foo() {} 

The Foo constructor is actually a function at this point, and Function.prototype is the actual Foo, the internal prototype, or __proto__ if you do.

 Foo.prototype = Foo; 

This only changes the prototype property of Foo, but not the internal prototype.

 Foo.constructor = Foo 

This actually only sets the constructor property to Foo, it does nothing with Foo.prototype.constructor, and it does nothing with the internal prototype constructor of Foo, which instanceof checks.


Try this code sequence, I hope it will be a little clearer:

 function Foo() { } ; (Foo.prototype != Function.prototype && Foo.__proto__ == Function.prototype); Foo.prototype = Foo; (Foo.prototype == Foo && Foo.__proto__ != Foo && Foo.__proto__ == Function.prototype); Foo.constructor = Foo; (Foo.constructor == Foo && Foo.prototype.constructor == Foo && Foo.__proto__.constructor != Foo); 
+1
source

All Articles