JavaScript | Data properties and Accessor properties

Source: Internet
Author: User

Property Type

Data Properties -A location that contains a data value that can read and write values

[writable]

Whether the value of the property can be modified

True

[enumerable]

Whether to return properties >for ( can be enumerated

True

[configurable]

can be removed by delete , can you modify the properties of the property, modify the accessor properties

True

[Value]

The data value that contains this property, read from this location when the value of the property is read.

Undefined

Accessor Properties

[enumerable]

Whether to return properties >for ( can be enumerated

true

[configurable]

can be removed by delete , can you modify the properties of the property, modify the accessor properties

true

[get]

function called when reading properties

undefined

[Set]

Functions that are called when a property is written

Undefined

Property Manipulation

    • To define a property: Object.defineproperty ()
    • To View properties: Object.getownpropertydescriptor ()

      <<script.js>>

"Use Strict";// *****************************************************************//Manipulating Data Propertiesvarperson ={name:' Hugh ', Age:29, Sayname:function() {Console.log (1); }}//Modify Property Default attribute: Object.defineproperty ()Object.defineproperty (person, "name", {writable:true, Value:' Dong ', Configurable:false, Enumerable:false}); Console.log (person) ;// *****************************************************************//Action Accessor PropertiesvarBook ={_year:2004,//_ As tags can only be accessed through the object propertyedition:0};object.defineproperty (book,"Year", {    //Accessor property year contains setter and getter functionsGetfunction() {        return  This. _year; }, set:function(newvalue) { This. _year =NewValue;  This. Edition = newValue-2004; }}) Book.year= 2008; console.log (book); Console.log (book.edition) ;//old method, IE8 part does not support DefineProperty ()//Strict mode is not available//Book._definegetter_ ("Year", function () {//return this._year;// });//Book._definesetter_ ("Year", function (newvalue) {//this._year = newvalue;//this.edition = newValue-2014;// });// *****************************************************************//Define multiple propertiesvarBook2 ={};object.defineproperties (book2, {//Data Properties_year: {value:2004, writable:false, Enumerable:false, Configurable:true}, Edition: {value:0, writable:false, Enumerable:false, Configurable:true    },    //Accessor PropertiesYear : {get:function() {            return  This. _year; }, set:function(newvalue) { This. _year =NewValue;  This. Edition = newValue-3000; }}); Console.log (BOOK2) ;// *****************************************************************//to view the properties of a propertyConsole.log (Object.getownpropertydescriptor (book2, ' _year ')) ); Console.log (Object.getownpropertydescriptor (Book2,' Edition ') ); Console.log (Object.getownpropertydescriptor (Book2,' year '));

JavaScript | Data properties and Accessor properties

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.