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:
- do not modify the object's __proto__ property.
- When you need to provide a prototype object for a new object, consider using the object.create .
Effective JavaScript Item 32 never modify __proto__