Effective JavaScript Item 32 never modify __proto__

Source: Internet
Author: User

this series as effective JavaScript 's reading notes.

and the object.getprototypeof compared to __proto__ is also reflected in its ability to modify the prototype inheritance chain of an object. Because it is a property, it can be set in addition to the operation that gets it.

However, never modify __proto__ . The reasons are as follows:

first, the most obvious reason is portability. Since not all JavaScript Execution Environments support this property, after using __proto__ , the code cannot be in those that do not support __proto_ _ Run in the environment.

Secondly, it is a performance consideration. the implementation of the JavaScript engine now has a lot of optimizations for the access to object properties, because these operations are most commonly used. When the __proto__ of an object is modified , it is equivalent to modifying the entire inheritance structure of an object, which causes many optimizations to be no longer available.

Finally, the most important reason is the need to ensure the reliability of the program. Since changing the __proto__ attribute, the object's prototype inheritance chain may be completely changed. Unexpected errors occur when other code in the program relies on the original inheritance chain. In general, the prototype inheritance chain needs to remain stable.

When you need to assign a prototype object to a newly created object, you can use the ES5 provided by object.create method. For environments that do not implement the ES5 standard, you can refer to Item, which gives a __proto__ that does not depend on the object.create the implementation of the method.

Summarize:

    1. do not modify the object's __proto__ property.
    2. When you need to provide a prototype object for a new object, consider using the object.create .

Effective JavaScript Item 32 never modify __proto__

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.