Summary of methods for obtaining prototype objects in Javascript, and javascript prototype
In Javascript, if we have an object but do not know its constructor, how can we obtain its prototype object?
In Chrome or FireFox, we can directly use the _ proto _ attribute of an object to obtain its prototype object.
Copy codeThe Code is as follows:
<! -- Lang: js -->
Function F (){};
Var foo = new F ();
Alert (foo. _ proto _ = F. prototype );
However, the __proto _ attribute is not supported in IE until IE11.
In browsers that do not support the _ proto _ attribute, how can we get the prototype object of the object? It can be obtained indirectly through constructor.
Copy codeThe Code is as follows:
<! -- Lang: js -->
Function F (){};
Var foo = new F ();
Alert (foo. constructor. prototype = F. prototype );
The constructor attribute is not an object's own attribute, but obtained from the prototype object along the prototype chain. This attribute points to the constructor corresponding to the prototype object. The prototype attribute of the constructor points to the prototype object, so we can obtain it indirectly.
The above section describes how to get the prototype object in javascript. I hope you will like it.