var myObject = { A:2 }; Object.getownpropertydescriptor (MyObject,"a"); { value:2, writable:true, emumerable:true, Configurable: true }
The property descriptor in this code is not only 2, but also writable (writable), enumerable (enumerable), configurable (configurable) , three kinds of
The property descriptor uses default values when creating normal properties , and we can also use object.defineproperty (...) . To add a new property or modify an existing attribute (if configurable) and set the attribute
code example:
var myObject = {}; Object.defineproperty (MyObject,"A", { value:2, writable:true, Configurable: true , emumerable:true }); Console.log (MYOBJECT.A); // 2
The following is an introduction to these three property descriptors:
1) Writable: Determines whether the value of the property can be modified
var myObject = {}; Object.defineproperty (MyObject,"A", { value:2, writable:false, Configurable: true , emumerable:true }); = 3; Console.log (MYOBJECT.A); // 2
parsing: Writable:false can be thought of as a property immutable , in strict mode ("use strict";), the engine throws a TypeError exception, which means we Cannot modify a non-writable property
2) Configurable: As long as the properties are configurable, you can use DefineProperty (...). method to modify the property descriptor
Attention!!
A) in the case of false, if the modification, whether it is not strict mode, will throw TypeError error
b) In this case, we can still change the state of the writable from true to False
c) The Delete property is also disabled (delete myobject.a;)
3) Emumerable: Enumerable, if it is set to false, this property will not appear in the enumeration, but it can be accessed normally
JS Attribute Descriptor