The first sentence of the article: “This is the second day of my participation in the More Wen Challenge.

Commonly used attributes

Data attributes

The so-called data attribute is the name of each attribute of the object. Data attribute has four characteristics as follows:

Different: indicates that the property can be configured, for example, by using delete and modifying the property. The default is true Enumerable: Indicates whether it can be enumerable, such as if a for-in loop is used to retrieve a property. The default is true. Writable: indicates whether it is writable and whether the value of an attribute can be modified. The default is true. Value: Data value containing the attribute. The default value is undefined.Copy the code

You can use object. GetOwnPropertyDescriptor (person, name) to obtain the description of the data attributes specified.

Accessor property Object.defineProperty

Accessor properties do not include data values. They include a list of getters and setters. By defining accessor properties, it is possible to set the value of one property to cause other properties to change.

let person = {
    name:"jack"
}
let temp = null;
Object.defineProperty(person,'name', {get:function(){
        console.log('Read')
        return temp;
    },
    set:function(val){
        console.log('被设置')
        temp = val
    }
})
person.name = 'lucy';
console.log(person.name)
Copy the code

Prototype method

Object. The prototype. The hasOwnProperty (” attribute name “)

This method returns a Boolean value indicating whether the property is present in the object. Attributes inherited from the prototype chain are not judged, like Object.key(), unlike for in.

Object.prototype.isPrototypeOf()

. This method is mainly to judge prototypeObj isPrototypeOf (object), namely whether object prototypeObj on the prototype chain of object object. Whether the object inherits from the prototypeObj.

* * Object. The prototype. The toString () method to return a * * says the Object of the string. Can be used to detect object types.

Object methods

Object.assign(obj)

Copying all enumerable properties of an object into a new object is a shallow copy.

Object.entries(),key(),value()

Returns the key-value pairs of the traversable properties of the given object

There is one key is needs to pay attention to the Object. The getOwnPropertyNames (), the Object. The keys (), the for – is available in the Object’s properties, but there is a difference between them is simple to summarize here.

For in: to use for… The in loop returns all enumerable properties accessible through the object, both in the instance and in the stereotype. Object.keys(): Used to get all the enumerable property values of the Object itself, but not the properties in the prototype, And returns an array of property names Object. GetOwnPropertyNames () : returns the Object of all its attributes of the attribute name (including an enumerated attribute) consisting of an array, but you don’t get on the prototype chain can attribute differences mainly in the property enumeration, came from the prototype, or instance.

object.getPrototypeOf(obj)

Returns the prototype of the specified object

The object.setPrototypeof () method sets the prototype of a specified Object to another Object or null.