JS Attribute Descriptor

Source: Internet
Author: User

var myObject = {            A:2        };        Object.getownpropertydescriptor (MyObject,"a");        {            value:2,            writable:true,            emumerable:true,            Configurable: true         }

The property descriptor in this code is not only 2, but also writable (writable), enumerable (enumerable), configurable (configurable) , three kinds of

The property descriptor uses default values when creating normal properties , and we can also use object.defineproperty (...) . To add a new property or modify an existing attribute (if configurable) and set the attribute

code example:

var myObject = {};        Object.defineproperty (MyObject,"A", {            value:2,            writable:true,            Configurable: true ,            emumerable:true        });        Console.log (MYOBJECT.A); // 2

The following is an introduction to these three property descriptors:

1) Writable: Determines whether the value of the property can be modified

var myObject = {};        Object.defineproperty (MyObject,"A", {            value:2,            writable:false,            Configurable: true ,            emumerable:true        });         = 3;        Console.log (MYOBJECT.A); // 2

parsing: Writable:false can be thought of as a property immutable , in strict mode ("use strict";), the engine throws a TypeError exception, which means we Cannot modify a non-writable property

2) Configurable: As long as the properties are configurable, you can use DefineProperty (...). method to modify the property descriptor

Attention!!

A) in the case of false, if the modification, whether it is not strict mode, will throw TypeError error

b) In this case, we can still change the state of the writable from true to False

c) The Delete property is also disabled (delete myobject.a;)

3) Emumerable: Enumerable, if it is set to false, this property will not appear in the enumeration, but it can be accessed normally

  

  

JS Attribute Descriptor

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.