Properties Description Object

Outline

JavaScript provides an internal data structure is used to describe properties of an object, controlling its behavior, such as whether the property is writable, and the like can be traversed. This internal data structure called "attributes describe the object" (attributes object). Attribute describes the properties of each object has its own corresponding store some information of the meta attribute.

{

  value: 123,

  writable: false,

  enumerable: true,

  configurable: false,

  get: undefined,

  set: undefined

}

Attribute describes the object attributes to provide 6 yuan.

(1)value

is the attribute value of the attribute value default is undefined.

(2)writable

writable is a Boolean value that indicates whether the attribute value (value) can be changed (that is, whether writable), the default is true.

(3)enumerable

enumerable is a Boolean value that indicates whether the property can be iterated, the default is true. If set to false, will cause certain operations (such as for ... in loop, Object.keys ()) This property is skipped.

(4)configurable

configurable is a boolean indicating configurable, default is true. If set to false, prevents certain operations to rewrite the property, such property can not be deleted, nor change the properties of an object description of the property (except for property value). In other words, configurable attribute controls can write attributes that describe the object.

(5)get

get is a function that indicates that the property value of the function (getter), the default is undefined.

(6)set

set is a function representing a function of the value stored in the attribute (the setter), default is undefined.

Object.getOwnPropertyDescriptor()

Object.getOwnPropertyDescriptor () method gets the object attribute description. Its first parameter is the target object, the second parameter is a string that corresponds to an attribute name of the target object.

var obj = {p 'a'};

Object.getOwnPropertyDescriptor(obj, 'p')

// Object { value: "a",

//   writable: true,

//   enumerable: true,

//   configurable: true

// }

The above code, Object.getOwnPropertyDescriptor () method to get the properties described objects obj.p. This method can only be used for object's own properties, not for inherited property.

 

Object.getOwnPropertyNames()

Object.getOwnPropertyNames method returns an array of all the members is the property name parameter object's own properties, regardless of whether the property can be iterated. This behavior Object.keys with different, Object.keys only return all the properties of the object itself can traverse the name attribute.

 

If the attribute exists, Object.defineProperty () method described in the updated object properties corresponding to the properties.

If a plurality of disposable defining or modifying properties may be used Object.defineProperties () method.

 

 

Element Properties

Property Description respective objects is called "Element Properties", as they can be seen as the property attribute control.

value

The value attribute is the value of the target property.

var obj = {};

obj.p = 123;

Object.getOwnPropertyDescriptor(obj, 'p').value

// 123

Object.defineProperty(obj, 'p', { value: 246 });

obj.p // 246

Property value by the above code, read or rewritten obj.p example.

writable

writable attribute is a Boolean value that determines the value of the target attribute (value) Can be changed.

var obj = {};

Object.defineProperty(obj, 'a', {

  value: 37,

  writable: false

});

obj.a // 37

obj.a = 25;

obj.a // 37

The above code, obj.a the writable attribute is false. Then, change the value obj.a will not have any effect.

Note that in the normal mode, not being given the assignment of writable attribute to false, only silently fail. However, strict mode error, even for a property re-assigned a same value.

The above code, is a proto prototype object, its properties are not written foo. obj object inheritance proto, then you can not customize this property a. If strict mode, this will throw an error.

However, there is a workaround is covered by property described objects around this limitation. The reason is that in this case, the prototype chain will be completely ignored.

enumerable

Enumerable (available ergodic) returns a Boolean value indicating whether the target property may be traversed.

Earlier versions of JavaScript, for ... in loop is based in operator. We know that, in the operator whether it is a property of the object itself or inherited, it will return true.

var obj = {};

'toString' in obj // true

In the above code, the object obj toString not own properties, but also in operator returns true, which leads toString property is also for ... in loop traversal.

This is clearly not reasonable, and later on the introduction of "traversable" of the concept. Only can traverse the property, it will be for ... in loop iterates, but also provides toString this class instance object inherits native attributes are not traversable, thus ensuring availability for ... in loop.

Specifically, if a enumerable attribute is false, the following three operations is not taken to the property.

for..in loop

Object.keys method

JSON.stringify method

configurable

Configurable (configurable) returns a Boolean value, determines whether the object can modify the attributes described. In other words, configurable when false, value, writable, enumerable and configurable not be modified. writable only false to true will complain, true to false is allowed. As for value, as long as there is a writable and configurable to true, it allows changes. In addition, writable is false, the direct target attribute assignment, not an error, but will not succeed.

 

Configurable attribute determines whether the target can be deleted (delete).

var obj = Object.defineProperties({}, {

  p1: { value: 1, configurable: true },

  p2: { value: 2, configurable: false }

});

 

delete obj.p1 // true

delete obj.p2 // false

 

 

Accessor

In addition to directly define other than a property can also define accessor (accessor). Wherein the setter stored-value function is called, using the object attribute description set property; value function is called getter, get object attributes using attribute description.

Once the target attribute defines the accessor, then the access time, will execute the corresponding function. Using this feature, you can achieve many advanced features, such as a property prohibited assignment.

var obj = Object.defineProperty({}, 'p', {

  get: function () {

    return 'getter';

  },

  set: function (value) {

    console.log('setter: ' + value);

  }

});

obj.p // "getter"

obj.p = 123 // "setter: 123"

In the above code, obj.p defined get and set properties. When obj.p value, will be called get; the assignment, it will call set.

JavaScript also provides another way of accessor.

var obj = {

  get p() {

    return 'getter';

  },

  set p(value) {

    console.log('setter: ' + value);

  }

};

The above written description of the object defined attributes are equivalent, and is more widely used.

Note that, the parameter values ​​can not accept get function, a function of stored values ​​can only accept a parameter set (i.e., attribute value). Access is often used, where the value of the object property depends on the internal data.

 

 

Control object state

Sometimes need to freeze the object read-write, the object is to prevent change. JavaScript provides three freezing methods, the weakest one is Object.preventExtensions, followed Object.seal, the strongest Object.freeze.

Object.preventExtensions way to make an object can not add new attributes.

Object.isExtensible method for checking whether an object using a method Object.preventExtensions. In other words, check whether you can add properties to an object.

Object.seal method allows an object either can not add new attributes can not delete the old property.

Object.isSealed method for checking whether an object using a method Object.seal.

Object.freeze way to make an object can not add new attributes, you can not delete the old property, you can not change the value of the property, making this object effectively becomes a constant.

Object.isFrozen method for checking whether an object using a method Object.freeze. One use Object.isFrozen is, after confirmation that an object is not frozen, and then its property assignment.

 

Guess you like

Origin www.cnblogs.com/hjy-21/p/12315449.html