Reference article:
- https://juejin.im/post/5b97903de51d450e3d2ca94c
- https://www.zhihu.com/question/40648241/answer/135240926
Using the Object.getOwnPropertyDescriptor
method 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.getOwnPropertyDescriptors
method 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 delete
delete the attribute, and whether to modify the properties characteristic descriptors, the default true
.
Is set to false
the unusable delete
delete attributes, given the strict mode directly; and can not be used defineProperty()
to modify the attribute descriptor methods, but can be used defineProperty()
methods writable
from the state true
to false
(a situation which can be modified); var
default 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 name
property 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 name
the properties of enumerable
look at:
Object.defineProperty(person, 'name', {enumerable: false})
person.propertyIsEnumerable('name')
false
Specifically, the enumerable
property will affect:
-
fon in
Whether loop can be traversed to the property -
Object.keys
Method to whether the attribute can take -
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 false
the assignment will fail silently, strict mode direct assignment error.
By Object.defineProperty()
changing the method attribute value
value is not affected, because this also means that reset writable
the attribute value false
.
Look at an example, name
the 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 setter
the function (however, these two functions are not necessary).
When read access properties, it will call getter
the function, when writing accessor property, will call the setter
function 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 _age
set the property get
and set
access to properties.