The prototype rule and the prototype chain in JS

Source: Internet
Author: User
Tags hasownproperty

1. Prototype rules

A. All reference types (arrays, objects, functions) have object attributes and are free to extend properties;
var arr = [];
ARR.A = 1;
B. All reference types (arrays, objects, functions) have a _proto_ attribute (implicit prototype), and the property value is an ordinary object;
C. All functions have a prototype (explicit prototype), and the attribute value is also a common object;
D. All reference types (arrays, objects, functions) whose implicit prototypes point to explicit prototypes of their constructors; (obj._proto_ = = = Object.prototype);
E. When trying to get a property of an object, if the object itself does not have this property, then it will go to its _proto_ (that is, its constructor of the prototype) to find;

2. Looping the properties of the object itself

When traversing an object's properties, the hasOwnProperty () method is used to determine whether a property is the property of an object itself; modern browsers have shielded the properties from the prototype in the for-in, but for the robustness of the program it is recommended to add this judgment:

 for inch obj) {          if(Obj.hasownproperty (item)) {                alert (item)            }}

3. Prototype chain
When trying to get a property of an object F, if the object itself does not have this property, it will go to its _proto_ (that is, its constructor prototype) Obj._proto_ to find, and when Obj._proto is not, it will be obj._proto. _proto_ (i.e., the constructor of the prototype of Obj's constructor is prototype);

The prototype rule and the prototype chain in JS

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.