Object method property description object

1. Attribute description object concept
JavaScript provides an internal data structure used to describe object attributes and control its behavior. This internal data structure is called the attribute description object. Each attribute has its own corresponding attribute description object, which saves some original information of the attribute.
example

{
    
    
	value:'111',//默认是undefined
	writable:true, //值为true或false  value值是否可改变
	enumberable:true, //  该属性是否可遍历 设置false则执行for...in循环或者Object.keys则会跳过该属性值
	configurable:true,//是否可配置修改这些元属性(即 writable/enumberable/configurable)
	get:undefined,//是一个函数 ,表示该属性的取值函数getter,默认为undefined
	set:undefined,//是一个函数,表示该属性的存值函数getter,默认为undifined	
}

2. Four commonly used functions to obtain properties
1. Object.getOwnPropertyDescriptor() and Object.getOwnPropertyDescriptors() to obtain the property description object of the object.
Insert picture description here
Both are the property description object of the obtained object. The syntax is as follows:

 Object.getOwnPropertyDescriptors(Array)
 Object.getOwnPropertyDescriptor(Array,key)

2. Obect.getOwnPropertyNames() returns an array of all property names in an object, regardless of whether the property can be traversed.
Example
Insert picture description here
3. Object.defineProperty() (modify a property) and Object.defineProperties (modify multiple properties).
This function allows Describe the object definition or modify one or more properties through properties and return the modified object
Object.defineProperty() usage example
Insert picture description here
Object.defineProperties() usage example
Insert picture description here
4. Object.prototype.propertylsEnumberable() returns a boolean value to determine a certain Whether each attribute can be traversed
Use example
Insert picture description here
Pay attention to the syntax when using

array.propertyIsEnumerable('+key+')

5. Meta-attribute changes

  • Writable is assigned in the case of false, the assignment is unsuccessful, no error will be reported under normal circumstances, but an error will be reported in strict mode, even if the original value is re-assigned. At the same time, the sub-object cannot customize its value.
    Note: If the sub-object wants to re-assign, you can re-overwrite the writable attribute of the sub-object.
  • Enumerable in the case of false for...in / Object.keys() / JSON.stringify() will not get the attribute (although the loop will not get it, but it can still be obtained directly)
  • If configurable is false, changing value/wrtable/enumerable/configurable will report an error, but it is allowed to change configurable from true to false. Direct assignment will not report an error but will not succeed (if configurable is true, the attribute can be deleted , False means the attribute cannot be deleted)

6.
Accessor In addition to direct definition, the attribute value can also be defined by accessor. The stored value function is called setter, the setter value described by the attribute is used, and the value function is called getter. The getter value described by the attribute is called getter.
Once the accessor is defined, the corresponding setter/getter function will be executed as long as the access is performed, which can be used for setting Many advanced features, such as prohibiting copying of an attribute.
Usage example
Insert picture description here
Note: You cannot define the value at the same time after setting the set and get functions
. Another type of
Insert picture description here
accessor is often used when the value of the attribute depends on the internal data of the object. .
7. Three freezing methods to control the state of
an object 1. Object.preventExtensions() makes it impossible for an object to add new properties
Object.isExtensible() to check whether the object uses this method.
2. Object.seal(), so that an object can neither add new properties nor delete old properties. This method does not affect the modification of the value of a certain property. You can use Object.isSealed() to check whether an object uses Object.seal()
3, Object.freeze(). Cannot add new attributes, cannot delete old attributes, cannot modify the attribute value, and make it a constant Object.isFrozen() can check whether Object.freeze() is
used. The limitations of the above three methods
can be changed by changing the prototype object To add attributes to the object

var obj = new Object();
Object.preventExtensions(obj);

var proto = Object.getPrototypeOf(obj);
proto.t = 'hello';
obj.t
// hello

At this time, you can freeze the prototype of obj as follows

var obj = new Object();
Object.preventExtensions(obj);

var proto = Object.getPrototypeOf(obj);
proto.t = 'hello';
obj.t
// hello

There is another limitation. The above method freezes the object pointed to by the attribute, but cannot freeze the content of the object itself, as shown in the following example:

var obj = {
    
    
  foo: 1,
  bar: ['a', 'b']
};
Object.freeze(obj);

obj.bar.push('c');
obj.bar // ["a", "b", "c"]

Guess you like

Origin blog.csdn.net/qq_40969782/article/details/115310204