Home  >  Article  >  Web Front-end  >  Summary of methods to obtain the prototype object of an object in Javascript_javascript skills

Summary of methods to obtain the prototype object of an object in Javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:13:131766browse

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 __proto__ attribute of the object to obtain its prototype object.

Copy code The code is as follows:


function F(){};
var foo = new F();
alert(foo.__proto__ == F.prototype);

However, the __proto__ attribute was not supported in IE until IE11.

So in a browser that does not support the __proto__ attribute, how do we get the prototype object of the object? Can be obtained indirectly through constructor.

Copy code The code is as follows:


function F(){};
var foo = new F();
alert(foo.constructor.prototype == F.prototype);

The constructor property is not a property of the object itself, but is obtained from the prototype object along the prototype chain. This property points to the constructor corresponding to this prototype object. The prototype attribute of the constructor points to the prototype object, so we can get it indirectly.

The above is the entire content of this article about obtaining prototype objects in JavaScript. I hope you will like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn