JavaScript data properties and accessor properties

Source: Internet
Author: User

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, which cannot be stored directly in the data value.

Data Attribute properties: Value, writable, enumerable, configurable.

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

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

Writable:true/false, whether the value of the property can be modified, false by default;

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

Accessor property attributes: Set, get, enumerable, configurable.

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

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

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.

Attributes that are added to an object or modify an existing property use the Object.defineproperty () or object.defineproperties () method;

Object.defineproperty (object, PropertyName, descriptor):

Parameter interpretation: object: The objects that need to add or modify properties;

PropertyName: Name of the property, 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 22

Emp.age = 25;

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 cannot 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 the value of _name by the Get method

Emp.name = ' Jery ';

Console.log (emp.name);//Output Tom, there is no set method, can not modify the value of _name

Object.defineproperty (EMP, ' age ', {

Configurable:true,

Get:function () {

return this._age;

}

Set:function (age) {

This._age = age;

}

});

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

Delete Emp.age;

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

Note: Accessor properties can be a good protection, when only the Get method, the implementation of read-only can not write, otherwise, only set, it can only write and cannot read

JavaScript data properties and accessor properties

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.