In Javascript, if we have an object but don't know its constructor, how do we get its prototype object?
In Chrome or FireFox browser, we can directly use the object's __proto__ attribute to get its prototype object.
The code copy 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 browser until IE11.
So in browsers that do not support the __proto__ attribute, how do we get the prototype object of the object? It can be obtained indirectly through constructor.
The code copy is as follows:
<!-- lang: js -->
function F(){};
var foo = new F();
alert(foo.constructor.prototype == F.prototype);
The constructor attribute is not the object's own attribute, but is obtained from the prototype object upwards along the prototype chain. This property points to the constructor corresponding to this prototype object. The prototype property of the constructor points to the prototype object, so we can get it indirectly.
The above is all about obtaining prototype objects in JavaScript. I hope you like it.