ES5 Data Property descriptor and Access descriptor

Source: Internet
Author: User

One, Data property descriptor

An object is a collection of properties, and the basic characteristics of an object are the property name (name) and the property value (value). ES5 adds a property descriptor that gives you finer control over the different operations of the property. Property descriptors are configurable, writable, and Enumerable.

Property descriptors are typically used in conjunction with object.defineproperty/object.defineproperties to define attributes, and are also subject to changes such as object.freeze/object.seal.

1. Configurable if and only if configurable is true, the property can be changed or deleted (delete), default to False

var obj = {}object.defineproperty (obj, ' name ', {value: ' John '})//cannot deletedelete Obj.name//Falseobject.defineproperty ( obj, ' name ', {configurable:true,value: ' John '})//Can deletedelete Obj.name//True

2. Writable if and only if writable is true, the property can be changed by the assignment operator (=) By default to False

var obj = {}object.defineproperty (obj, ' name ', {value: ' john '}) Obj.name = ' Backus '//modification does not work, still John, error in strict mode prevents modification of Object. DefineProperty (obj, ' name ', {writable:true,value: ' John '}) obj.name = ' Backus '//was changed to Backus

3. Enumerable if and only if enumerable is true, the property can appear in the object's enumeration property (for in), which defaults to False

var obj = {}object.defineproperty (obj, ' name ', {value: ' John '})//cannot traverse for (var a in obj) {console.log (a)//no output}object.def Ineproperty (obj, ' name ', {enumerable:true,value: ' John '})//Can traverse for (var a in obj) {console.log (a)//output "name"}

ES6 Object.keys only returns the properties of Enumerable=true

var obj = {name: ' John '}object.defineproperty (obj, ' name ', {value: ' Backus ', enumerable:true}) Object.defineproperty ( obj, ' age ', {value:30,enumerable:false}) Object.keys (obj)//[' name ']

The enumerable value of the property can be judged by the propertyIsEnumerable method

Obj.propertyisenumerable (' name ')//trueobj.propertyisenumerable (' age ')  //False

4. Use ES3 (Traditional) JSON to define the object, its configurable/writable/enumerable default is true, as follows

var obj = {name: ' John ', age:30}//configurabledelete obj.name//true//Writableobj.age = +//true//enumerablefor (VA R A in obj) {console.log (a)//age}

Also that

var obj = {name: ' John ', age:30}

Equivalent to

Object.defineproperty (obj, ' name ', {value: ' John ', configurable:true,writable:true,enumerable:true}) Object.defineproperty (obj, ' age ', {value:33,configurable:true,writable:true,enumerable:true})

  

5. Define an object using ES5 's Object.defineproperty/object.defineproperties method, whose configurable/writable/enumerable defaults to False, as follows

var obj = {}object.defineproperty (obj, ' name ', {value: ' John '}) object.defineproperty (obj, ' age ', {value:33})// Configurabledelete obj.name//false//Writableobj.age = +//false//enumerablefor (var a in obj) {console.log (a)//no output , cannot traverse}

Also that

Object.defineproperty (obj, ' name ', {value: ' John '})

Equivalent to

Object.defineproperty (obj, ' name ', {value: ' John ', configurable:false,writable:false,enumerable:false})

The Data property descriptor is summarized as follows

Second, Access attribute descriptor

An access descriptor is a property described by a pair of getter-setter functions, in the form

Name: {get:function () {...},set:function (newval) {...},enumerable:true,configurable:true}

For example

var obj = {}object.defineproperty (obj, ' name ', {configurable:true,enumerable:true,get:function () {console.log (' get ') Return This.value},set:function (newval) {console.log (' set ') This.value = newval}})//assignment calls the Set method Obj.name = ' John '//value is adjusted Using the Get method obj.name

With the above attribute descriptor can only exist one, that is, two select one, can not exist at the same time, otherwise it will be an error

var obj = {}//error mode one object.defineproperty (obj, ' name ', {value: ' John ', get:function () {console.log (' get ') return This.value}})//Error mode two object.defineproperty (obj, ' name ', {writable:true,get:function () {console.log (' get ') return This.value}})

Firefox error is as follows

The access descriptor is summarized as follows

Iii. several functions related to property descriptors
    1. Object.defineproperty
    2. Object.defineproperties
    3. Object.getownpropertydescriptor

Object.defineproperty has been described above, Object.defineproperties Bulk custom object properties, internal actually loop way call Object.defineproperty

Object.defineproperties (obj, {name: {value: ' John ', writable:true},age: {value:30,enmuerable:true}})

Object.getownpropertydescriptor returns a descriptor for a property of the object, which is itself an object

Output

ES5 Data Property descriptor and Access 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.