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
Instance attributes and prototype attributes
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.
js enumeration
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
##Attribute descriptor There are two main forms: Data descriptor and Access descriptor.
Use the
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
* }
*/
Copy after login
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.
getGet the accessor function (
getter) of this property. If there is no accessor, the value is undefined. (Only valid for property descriptions containing accessors or setters)
setGet the setter function (
setter) of the property. If there is no setter, the value is undefined. (Only valid for property descriptions containing accessors or setters)
configurable
configurableAttributes indicate whether the object's properties can be deleted, and except writable Whether other characteristics besides characteristics can be modified.
enumerable
enumerableDefines whether the properties of the object can be used in for...in loops and Object.keys() is enumerated.
name,_age has
'configurable', 'enumerable', 'value',' writable'Four attribute descriptors, collectively referred to as data descriptors
age has
'configurable', 'enumerable ', 'get', 'set' four attribute descriptors, collectively referred to as access descriptor
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!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn