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