Prototype approach (static approach)
- Object.assign()
The object.assign () method is used to assign the values of all enumerable properties from one or more source objects to the target Object, which it returns.
Grammar:
Object.assign(target, ... sources)// Parameters: target: target object, sources: source object
Copy the code
Have a lychee:
var obj1 = {name:'Marry'.id:0},
obj2 = {age:18.address:'Beijing'.name:'Jack'};
Object.assign(obj1,obj2); // {id:0,age:18,address:' Beijing ',name:'Jack'}
// If obj1 and obj2 have the same name, obj2 overwrites obj1's; Otherwise, keep all properties of obj1 and add all properties of obj2
Copy the code
- Object.defineProperty()
The object.defineProperty () method directly defines a new property on an Object, or modifies an existing property of an Object, and returns the Object.
Grammar:
Object.defineProperty(obj, prop, descriptor)
// Parameters: obj: the object to define attributes, prop: the name of the attribute to define or modify, or Symbol,descriptor: the attribute descriptor to define or modify
Copy the code
Have a lychee:
var obj = {name:'Jack'.age:18};
Object.defineProperty(obj,'age', {value:38}); // {name: "Jack", age: 38}
Object.defineProperty(obj,'address', {value:'ChengDu'}); // {name:"Jack",age:38,address:'ChengDu'}
Copy the code
- Object.keys()
The object.keys () method returns an array of a given Object’s own enumerable properties in the same order as the property names would be returned if the Object were iterated through normally
Grammar:
Object.keys(obj)
// Argument: obj: the object to return with its enumeration property
Copy the code
Have a lychee:
var obj = {name:'Jack'.age:18};
Object.keys(obj); // ["name", "age"]
Copy the code
- Object.values()
The object.values () method returns an array of all the enumerable property values of a given Object itself, in the same order as the values used for… The in loop has the same order (the difference is that for-In loops enumerate properties in the stereotype chain).
Grammar:
Object.values(obj)
// Argument: obj: the object that is returned with an enumerable property value
Copy the code
Have a lychee:
var obj = {name:'Jack'.age:18};
Object.values(obj); // ["Jack", 18]
Copy the code
Instance methods
- Object.prototype.hasOwnProperty()
The hasOwnProperty() method returns a Boolean value indicating whether the object has the specified property in its own properties (that is, whether it has the specified key).
Grammar:
obj.hasOwnProperty(prop)
// Parameter: prop: the String name of the property to be tested, or Symbol.
Copy the code
Have a lychee:
var obj = {name:'Jack'.age:18};
obj.hasOwnProperty('name'); // true
obj.hasOwnProperty('toString'); // false because it is a prototype method
Copy the code
- Object.prototype.toString()
The toString() method returns a string representing the object.
Grammar:
obj.toString();
Copy the code
Have a lychee:
var obj = {name:'Jack'.age:18},
arr = [1.2];
obj.toString(); // "[object Object]"
arr.toString(); / / 1, 2, ""
Copy the code
–End–