Home > Article > Web Front-end > Detailed explanation of traversal problems in js
The content shared with you in this article is about the detailed explanation of traversal issues in js. It has certain reference value. Friends in need can refer to it
Object properties in JavaScript are divided into two types: Data properties and Accessor properties.
According to the specific context, attributes can be divided into: Prototype attributes and Instance attributes .
Prototype attributes are attributes defined in the object’s prototype prototype
,
Instance attributes On the one hand, they come from the constructor function, and then there are new attributes added after the constructor is instantiated.
Traversing the properties of an object in JavaScript is not very simple, mainly for two reasons:
Objects in JavaScript are usually in a prototype chain, and they will inherit some properties from one or more upper prototypes
The properties in JavaScript not only have values, It also has some other characteristics besides value. One of the characteristics that affects attribute traversal is Enumerable
(an attribute descriptor). If the value is true
, then this attribute is Enumerated, otherwise
##Attribute descriptor There are two main forms:
Data descriptor and
Access descriptor.
Object.getOwnPropertyDescriptor and
Object.getOwnPropertyDescriptors methods to obtain the
property descriptor of the object.
var obj = { name: '10', _age: 25, get age(){ return this._age; }, set age(age){ if(age<1){ throw new Error('Age must be more than 0'); }else{ this._age = age; } } }; var des = Object.getOwnPropertyDescriptors(obj); console.log(des); /** * des: { * name: { * configurable: true, * enumerable: true, * value: "10", * writable: true, * __proto__: Object * }, * _age: { * configurable: true, * enumerable: true, * value: 25, * writable: true, * __proto__: Object * }, * age: { * configurable: true, * enumerable: true, * get: f age(), * set: f age(age), * __proto__: Object * }, * __proto__: Object * } */valueThe value of this attribute (valid only for data attribute descriptors)writableWhen
When the writable attribute is set to
false, the property is said to be "unwritable". It cannot be reassigned.
getter) of this property. If there is no accessor, the value is
undefined. (Only valid for property descriptions containing accessors or setters)
setter) of the property. If there is no setter, the value is
undefined. (Only valid for property descriptions containing accessors or setters)
configurableAttributes indicate whether the object's properties can be deleted, and except
writable Whether other characteristics besides characteristics can be modified.
enumerableDefines whether the properties of the object can be used in
for...in loops and
Object.keys() is enumerated.
'configurable',
'enumerable',
'value',
' writable'Four attribute descriptors, collectively referred to as
data descriptors
'configurable',
'enumerable ',
'get',
'set' four attribute descriptors, collectively referred to as
access descriptor
'configurable' | 'enumerable' | 'value' | 'writable' | 'get' | 'set' | |
---|---|---|---|---|---|---|
yes | yes | yes | yes | no | no | |
yes | yes | no | no | yes | yes |
The above is the detailed content of Detailed explanation of traversal problems in js. For more information, please follow other related articles on the PHP Chinese website!