Summary of methods for obtaining prototype objects in Javascript, and javascript prototype

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.