On JavaScript data attribute and accessor property _javascript skill

Source: Internet
Author: User
Tags string format

The properties of an object in ES5 can be divided into ' data properties ' and ' accessor properties '.

Data properties are typically used to store data values, and accessor properties correspond to set/get operations and cannot store the data value directly.

data Attribute attributes:value, writable, enumerable, configurable.

Explanation:configurable:true/false, whether you can delete attributes through Delete, can modify the attributes of properties, whether the property can be modified to accessor properties, default false;

Enumerable:true/false, whether it can be returned through a for in loop, default false;

Writable:true/false, whether you can modify the value of the property, default false;

value:undefined, sets the value of the property, the default undefined.

Accessor Property attributes:set, get, enumerable, configurable.

Explanation:configurable:true/false, whether you can delete attributes through Delete, can modify the attributes of properties, whether the property can be modified to accessor properties, default false;

Enumerable:true/false, whether it can be returned through a for in loop, default false;

Set:function, the function that is called when the property value is read;

Get:function, the function that is called when the property value is modified.

Add attributes to an object or modify the attributes of an existing property using the Object.defineproperty () or object.defineproperties () method;

Object.defineproperty (object, PropertyName, descriptor):

Parameter interpretation: object: Objects that need to be added or modified;

PropertyName: The name of the attribute, the string format;

Descriptor: A description of the property that sets the attributes of the data property or accessor property.

Example Analysis:

Data properties:

var emp = {

name: ' Tom '

};

 

Object.defineproperty (EMP, ' name ', {

writable:false

});

Emp.name = ' jery ';
Console.log (emp.name)//Output Tom because the writable is set to False



Object.defineproperty (EMP, ' age ', {

configurable: False,

writable:true,

value:22

});

Console.log (emp.age)//output 22, because the value is set to

emp.age =;

Console.log (emp.age);//output 25, set writable to True

delete emp.age;

Console.log (emp.age);//output 25, set configurable to False, this property can not be deleted

Accessor properties:


var emp ={

_name: ' Tom ',

_age:20

};



Object.defineproperty (EMP, ' name ', {

get:function () {return

this._name;

}

});

Console.log (emp.name);//Output Tom, return _name value by Get method

Emp.name = ' jery ';

Console.log (emp.name)//Output Tom, there is no set method to modify _name value

 

object.defineproperty (EMP, ' age ', {

configurable: True,

get:function () {return

 this._age

}

Set:function (age) {

this._age = age;

}

});

Emp.age =;
Console.log (emp.age)/output 25,emp.age=25 is using the Set method to assign 25 to _age,emp.age is to use the Get method to read the _age out

delete emp.age;

Console.log (emp.age);//Output undefined,configurable True, you can use the Delete method to delete the Emp.age property

Note: Accessor properties can be very protective, and when there is only a GET method, the read-only cannot be written;

The above is a small series for everyone to talk about JavaScript data properties and accessor properties all the content, I hope that we support cloud Habitat Community ~

Related Article

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.