The prototype chain in JavaScript

Source: Internet
Author: User

The prototype object is also a normal object, is an object with an implicit __proto__ property, the prototype may have its own prototype, if the prototype of a prototype object is not NULL, we call it a prototype chain. A prototype chain is a (finite) object chain of objects that are used to inherit and share attributes.

Properties in a prototype object can be shared by multiple instances. The prototype object exists in the constructor's properties (prototype), and the prototype value is an object type data (object).

What are the property values for JavaScript data objects, and give a few simple chestnuts:

Writable: Whether the value of this property can be changed;

Configurable: Whether the configuration of this property can be deleted, modified;

Enumerable: Whether this property can be traversed in the for...in loop or listed in the Object.keys.

Value: The value of the property.

When you build an object, you place the method in the prototype object of the constructor, which resolves the memory redundancy problem caused by creating multiple instances of the object. (The principle of resolving memory redundancy is because the properties in the prototype object can be shared by multiple instances)

The lookup rule for the prototype chain is this: When invoking an object's properties, look for the property from the instance itself, and use it directly if it can be found, or look in the prototype object of the instance, and if it cannot be found in the prototype object, it will continue to find the prototype object under the prototype object. The dynamic Create property is executed until the prototype of the object base class is found, or if it is not found. (The prototype object described above refers to the __proto__ attribute)

In JavaScript, there is a function that, when performing an object lookup, never finds a prototype, this function is hasOwnProperty

When an attribute in an object is assigned a value, it is first looked up from the instance itself, and the attribute in the instance overrides the attribute value in the instance, otherwise the attribute is created directly in the instance. (does not affect the same name attribute in the prototype)

The prototype chain in JavaScript

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.