Explanation of attribute descriptors in ECMAScript5 _ basic knowledge

Source: Internet
Author: User
This article mainly introduces the detailed description of attribute descriptors in ECMAScript5. This article describes the Object. defineProperty, data descriptor, access descriptor, get attribute descriptor, object control, and so on. if you need it, refer to attribute descriptor as a new concept in ES5, its function is to add more control over object attributes.

Object. defineProperty

To study attribute descriptors, first talk about the Object. defineProperty method. This method defines new attributes for the object or modifies existing attributes. The prototype is as follows:

The code is as follows:


Object. defineProperty (obj, prop, descriptor)


Example:

The code is as follows:


Var obj = {};
Object. defineProperty (obj, 'attr', {value: 1 });


The above code adds an attribute named attr to the obj object, and the value is 1. Equivalent:

The code is as follows:


Var obj = {};
Obj. attr = 1;


In comparison, the Object. defineProperty syntax seems more complex. However, its biggest mystery lies in its third parameter.

Data descriptor

Suppose we want attr to be a read-only attribute, we can add writable data descriptor:

The code is as follows:


Var obj = {};
Object. defineProperty (obj, 'attr ',{
Value: 1,
Writable: false
});
Console. log (obj. attr );
Obj. attr = 2; // fail
Console. log (obj. attr );


After executing the above program, we can find that the attr value printed twice is 1, that is, writing to the attribute fails. However, this result may be a bit confusing, because the execution of the value assignment statement is not abnormal, but fails. Imagine if such a problem occurs in a large piece of code, it will be hard to find out. In fact, as long as the code runs in strict mode, exceptions will occur:

The code is as follows:


'Use strict '; // enter the strict mode
Var obj = {};
Object. defineProperty (obj, 'attr ',{
Value: 1,
Writable: false
});
Obj. attr = 2; // throw exception


Next let's take a look at another data descriptor enumerable, which can control whether the attribute can be enumerated. If you simply define an attribute, it can be enumerated in the for... in loop:

The code is as follows:


Var obj = {};
Obj. attr = 1;
For (var I in obj) {console. log (obj [I]);}
Enumerable can "hide" it:

Var obj = {};
Object. defineProperty (obj, 'attr ',{
Value: 1,
Enumerable: false
});
For (var I in obj) {console. log (obj [I]);}


After executing the above code, you will find that nothing is output on the console, because the attr attribute cannot be enumerated.

At this point, you may have a question: can the attribute descriptor be modified? For example, can a read-only attribute be defined as writable? In fact, this depends on another data descriptor retriable, which can control whether the attribute descriptor can be changed.

The code is as follows:


Var obj = {};
Object. defineProperty (obj, 'attr ',{
Value: 1,
Writable: false,
Retriable: true
});
Object. defineProperty (obj, 'attr ',{
Writable: true
});
Obj. attr = 2;


The above code first defines attr as a read-only attribute, and then defines it as writable. Therefore, the write to attr is successful.

Access descriptor

The access descriptor is similar to the get/set accesser in the object-oriented method.

The code is as follows:


Var obj = {};
Object. defineProperty (obj, 'attr ',{
Set: function (val) {this. _ attr = Math. max (0, val );},
Get: function () {return this. _ attr ;}
});
Obj. attr =-1;
Console. log (obj. attr); // 0


In the above code, access to attr actually becomes access to _ attr, and the set function limits the minimum value to 0.

Get attribute descriptor

As described above, we have set attribute descriptors. how can we get the configured descriptors? Object. getOwnPropertyDescriptor can complete this task.

The code is as follows:


Var obj = {};
Object. defineProperty (obj, 'attr ',{
Value: 1,
Writable: false,
Retriable: true
});
Var desc = Object. getOwnPropertyDescriptor (obj, 'attr ');
Console. dir (desc );

Object control

The Object. defineProperty mentioned above operates on the attributes of the Object, while the three methods described below operate on the Object directly.

Object. preventExtensionsThe object cannot have new attributes:

The code is as follows:


Var obj = {};
Obj. attr = 1;
Object. preventExtensions (obj );
Obj. attr2 = 2; // fail


Object. sealOnly the attribute values of an object can be modified. (if the attribute is read-only, the attribute values cannot be modified ):

The code is as follows:


Var obj = {};
Obj. attr = 1;
Object. seal (obj );
Obj. attr = 1.5;
Delete obj. attr; // fail


Object. freezeThe object cannot be modified at all:

The code is as follows:


Var obj = {};
Obj. attr = 1;
Object. freeze (obj );
Obj. attr= 1.5; // fail
Obj. attr2 = 2; // fail


Then you may ask, how do you know if an object has been preventExtensions, seal, or freeze? The answer is to call Object. isExtensible, Object. isSealed, and Object. isFrozen respectively. the usage of these three functions is simple and it is no longer cumbersome.

In general, attribute descriptors can further strictly control objects and enhance the rigor of program logic. The only drawback is that ES5 is basically implemented in IE9 (IE9 does not support strict mode ), considering that the domestic Internet Explorer 8 shares are still relatively high, this set of things can only be used in mobile browsers and Node. js is used.

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.