2020.4.17

6.1 Three properties of objects

Prototype: points to another object, the properties of this object inherit from it.

Class class: A string identifying the type of object, such as: "[Object object]". The custom class is also displayed as object, so it cannot be judged by class.

Extensible flag: whether to add new attributes to this object.

Explanation of several terms:

Built-in object native object: a class defined by the js specification. Such as array, function, date, regular.

Host object: The host environment in which the js interpreter is embedded, such as a browser.

User-defined object: an object created by js at runtime.

Own property: property defined directly in the object.

Inherited property: The property defined by the object's prototype object.

6.2.3 Scenarios where setting property p for object o will fail

The p attribute in o is read-only. The exception is the use of defineProperty ().

The p attribute in o is inherited and is read-only. You cannot override read-only inherited attributes with free attributes of the same name.

o There is no own attribute p and no inherited attribute p, and o extensibility is false.

6.3 Delete attribute

Delete simply disconnects the attribute from the host object, and does not manipulate the attribute.

a={p:{x:1}}
b=a.p
delete a.p
/ * After execution bx == 1. Need to traverse the attributes of the deleted object and delete recursively * /

6.4 Detection attributes

In the left is the attribute name (string), and the right is the object. Returns true if the object's own attribute or inherited attribute exists.

The object's hasOwnProperty checks whether it has its own properties.

The object's propertyIsEnumerable is an enhanced version of hasOwnProperty, the detection is its own property and can be enumerated.

Note that if you use o [p]! == undefined to detect an attribute, an attribute with a value equal to undefined will be misjudged. At this time can only be judged with in.

6.5 Enumeration properties

The for in loop will enumerate inherited properties and methods, which need to be filtered with hasOwnProperty and typeof! == "function".

Object.keys () returns the name array of the object's own enumerable properties.

Object.getOwnPropertyNames () returns more non-enumerable properties than Object.keys ().

6.6 Attribute getters and setters

Read / write properties: there are setter and getter methods

Read attributes: only getter

Write property: only setter, read value returns undefined

var o = {
	/ * Define a read-write property data_prop * /
	get data_prop(){},
	set data_prop(val){},
	/ * Define a read-only attribute read_only * /
	get read_only(){},
	/ * Define a write-only attribute write_only * /
	set write_only (val) {}, 
     / * Do n’t forget the trailing comma * / }

Note that this in the getter and setter methods refers to the object, that is, the getter and setter in different o points to different o. You can access other properties of the object through this.

6.7 Properties of attributes

The properties of the property are value, writable, enumerable, and configurable.

js uses the "property descriptor" property discriptor object to describe the above four characteristics. Use Object.getOwnPropertyDiscriptor (objectName, propertyName) to get the property object with its own properties.

If the attribute is defined by a general method (o: {x: 1} or x in o ['x'] = 1), then return {value:, writable: boolean, enumerable: boolean, configurable: boolean}

If the property is defined by a getter or setter, it returns {set: function, set: function, enabler: boolean, configurable: boolean}

Use Object.defineProperty (objectName, propertyName, propertyDiscriptor) to create or modify the properties of an object, where propertyDiscriptor can be one of the above two.

defineProperties is passed in (objectName, {propertyName: propertyDiscriptor, propertyName2: propertyDiscriptor2}) to modify multiple properties at once.

6.8.1 Prototype properties

Object.isPrototypeOf () is very similar to instanceOf, the function acts as its name.

6.9 Serializing objects

JSON.parse retains the string format in date format and does not restore it to a date object. Similarly, functions, RegExp, Error, and undefined values ​​cannot be serialized and restored.

JSON.stringify only serializes its own enumerable properties.

 

Guess you like

Origin www.cnblogs.com/lpjworkroom/p/12717232.html