Attribute describes the object (Property Descriptor)

Reference article:

  1. https://juejin.im/post/5b97903de51d450e3d2ca94c
  2. https://www.zhihu.com/question/40648241/answer/135240926




Using the Object.getOwnPropertyDescriptormethod described in acquiring properties:

let person = {name: 'tom', age: 16};
let descriptor = Object.getOwnPropertyDescriptor(person, 'name');
console.log(descriptor );

result:

{
  value: "tom", 
  writable: true, 
  enumerable: true, 
  configurable: true
}

Using the Object.getOwnPropertyDescriptorsmethod of acquiring all the attributes described:

let person = {name: 'tom', age: 16};
let descriptor = Object.getOwnPropertyDescriptors(person);
console.log(descriptor );

result:

{
age: {value: 16, writable: true, enumerable: true, configurable: true}
name: {value: "tom", writable: true, enumerable: true, configurable: true}
}




Data attribute (Data Properties)

  • Configurable (configurable)

Configurability can be used to decide whether to deletedelete the attribute, and whether to modify the properties characteristic descriptors, the default true.

Is set to falsethe unusable deletedelete attributes, given the strict mode directly; and can not be used defineProperty()to modify the attribute descriptor methods, but can be used defineProperty()methods writablefrom the state trueto false(a situation which can be modified); vardefault statement is variable false.

example:

let person = {name: 'tom', age: 16};
Object.defineProperty(person, 'name', {configurable: false});
delete person.name;
console.log(person)
// {name: "tom", age: 16}

Visible nameproperty is not deleted.

  • enumerable (enumerable property)

Enumerable decisions property appears in the property of the enumerator object.

Ordinary user-defined default attribute is enumerable, while the native default inherited property is not enumerable.

Use propertyIsEnumerable()can be determined whether the properties of an object can be enumerated:

let person = {name: 'tom', age: 16};

person.propertyIsEnumerable('name')
// true

We modify namethe properties of enumerablelook at:

Object.defineProperty(person, 'name', {enumerable: false})

person.propertyIsEnumerable('name')
false

Specifically, the enumerableproperty will affect:

  1. fon in Whether loop can be traversed to the property
  2. Object.keys Method to whether the attribute can take
  3. JSON.stringify Method to whether the attribute can take
  • value (attribute value)

When the attribute value of this attribute contains the value of the data, read the property value, is read from this position, when the attribute value is written, the new value is stored in this location. Default undefined.

  • writable (writability)

You can write decisions can change the value of the property, by default true; set falsethe assignment will fail silently, strict mode direct assignment error.

By Object.defineProperty()changing the method attribute valuevalue is not affected, because this also means that reset writablethe attribute value false.

Look at an example, namethe attribute has not been modified:

let person = {name: 'tom', age: 16};
Object.defineProperty(person, 'name', {writable: false})
person.name = 'john'
console.log(person)
// {name: "tom", age: 16}




Access Properties (Accessor Properties)

Access Properties does not contain data values which includes a pair of children getter, and setterthe function (however, these two functions are not necessary).

When read access properties, it will call getterthe function, when writing accessor property, will call the setterfunction and pass the new value.

example:

let person = {name: 'tom', _age: 16};

Object.defineProperty(person, 'age', {
  get: function() {
    return this._age;
  },
  set: function(newValue) {
    this._age = newValue;
  }
})

person.age = 18;
console.log(person.age)
// 18

Here we _ageset the property getand setaccess to properties.

Guess you like

Origin blog.csdn.net/weixin_33753845/article/details/90801855