[Effective JavaScript note] 44th: Using a null prototype to prevent prototype contamination

Source: Internet
Author: User

The 43rd article mentions even if uses the direct instance of the object, also cannot completely avoid, the Object.prototype object modifies, causes the prototype pollution. One of the simplest ways to prevent prototype contamination is to not use prototypes. Before ES5, there was no standard way to create a new object for an empty prototype.

Try

Set the prototype property of the constructor to null or undefined

function C(){}C.prototype=null;
Results

Instantiation of the constructor still gets an instance of object.

var c=new C();Object.getPrototypeOf(c) === null;//falseObject.getPrototypeOf(c) === Object.prototype;//true
ES5 Standard Method

ES5 provides a standard way to create an object without a prototype. The Object.create function can construct objects dynamically using a user-specified prototype chain and a property descriptor. The property descriptor describes the value and attributes of the new object property. By simply passing a null prototype argument and an empty descriptor, you can create a real empty object.

var x=Object.create(null);Object.getPrototypeOf(x)==null;//true

Prototype contamination cannot affect such an object.

Compatible version

Some old JS environments that do not support the Object.create function may support the __proto__ property, and object literals also support initializing a new object with a prototype chain of NULL.

var x={__proto__:null};x instanceof Object;//false

The above method is also very effective, but after the standard method, the standard method is a better choice.

Attention

The __proto__ property is non-standard and not all environments can be ported. JS's implementation is not guaranteed to support it later, so there are standard methods, try to consider the standard method first.
As can be seen, although __proto__ can solve the problem, but also introduce its own platform incompatibility problem, prevent the free prototype object as a truly robust dictionary implementation. A more robust approach, which is mentioned in the next article.

Tips
    • In a ES5 environment, empty objects created with object.create (null) are less susceptible to contamination.

    • In some older environments, consider using {__proto__: null}

    • Note, however, that __proto__ is neither standard nor fully portable, and may be removed in the future JS environment

    • Never use the "__proto__" As a dictionary key, because some environments treat it as a special attribute.

[Effective JavaScript note] 44th: Using a null prototype to prevent prototype contamination

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.