Property properties of JavaScript objects

  • 2020-03-30 02:28:21
  • OfStack

The property of an object in JavaScript has three properties:
1. The writable. Whether the property is writable.
2. Enumerable. Whether the property is enumerated when using the for/in statement.
3. The configurable. Whether the property property can be modified, whether the property can be deleted.

In the ECMAScript 3 standard, the above three properties are true and immutable: the property of the new object is writable, enumerable, and deletes; In the ECMAScript 5 standard, these properties can be configured and modified through property descriptors.

If the property value of the information as the attribute of property, property of object has four attributes: the value, writable, enumerable and configurable.

With getter and setter methods to define the property, because its not writable attribute (whether the property can be written depends on whether a setter method exist), so this property also has four properties: get, set, enumerable and configurable - get and set the value of the attribute of the function.

Gets the property of the object property

ECMAScript 5 standard, can pass the Object. GetOwnPropertyDescriptor () to obtain the Object itself a property attribute information:

var o = {x:1};
var a = Object.create(o);
a.y = 3;
console.log(Object.getOwnPropertyDescriptor(a, "y"));//Object {configurable=true, enumerable=true, writable=true, value=3}
console.log(Object.getOwnPropertyDescriptor(a, "x"));//undefined

As you can see, if the property does not exist or if the property is inherited from the prototype object, undefined is returned.

Sets the property of the object property

In the ECMAScript 5 standard, you can set the property of an Object's own property by using object.defineproperty () :

Object.defineProperty(a, "y", {

If the property set is inherited from the prototype object, then JavaScript will create a property with the same name in the object itself, which is consistent with the behavior of the assignment operation:

Object.defineProperty(a, "x", {

In addition to modifying the property property, you can also change the property to be accessed with a getter or setter:

Object.defineProperty(a, "y", {
    get:function(){return 42;}

When using object.defineproperty (), property values in the property description Object can be partially ignored. When property values are ignored, the following rules are used in JavaScript:

If the property is new, all ignored property values are false or undefined.
If the property already exists, all ignored property values remain unchanged.

Batch sets the property of the object property

If you need to set the properties of more than one property at once, you can use the object.defineproperties () statement. This statement returns the modified object.

Object.defineProperties(a, {
    "y":{value:79, writable:true, enumerable:true, configurable:true},
    "z":{value:99, writable:true, enumerable:true, configurable:true}
console.log(a);//Object {y=79, z=99}

Property property setting rules

When modifying a property property, the following rules must be followed. If the rule is violated, JavaScript will report a TypeError:

If the object is not extensible, you can only modify the properties of an existing property, not add a new property.
If the property of the configurable attribute to false, cannot modify configurable and enumerable attribute's value, for writable attribute, it can be changed from true to false, but not from the false to true. If the property is defined by the getter and setter, the getter and setter methods cannot be modified.
If the property of the configurable and writable attribute to false, then the property values cannot be changed. If the property of the writable attribute to false, but its configurable attribute to true, the property value is still can be modified.

Related articles: