This is the first day of my participation in Gwen Challenge
1. Object.assign()
Used to clone
var first = {name : 'kong'};
var last = {age : 18};
var person = Object.assign(first, last);
console.log(person);//{name : 'kong', age : 18}
Copy the code
2. Object.is()
Used to determine whether two values are the same
Object.is(a, b); // Return true or false
// Note that, unlike the == operator, this function does not cast any type,
// It should be more like ===, but it's worth noting that it treats +0 and -0 as different
Object.is(+0, -0) // false
Object.is(NaN.NaN) // true
Object.is(NaN.0/0) // true
+0= = = -0 // true
NaN= = =NaN // false
Copy the code
3. Object.keys()
The name used to return the enumerable properties and methods of the object
var a = {name : 'kong'.age : 18.func : function(){}};
Object.keys(a); //['name', 'age', 'func']
Copy the code
4. isPrototypeOf()
Determines whether an object exists in the prototype chain of another object
function a(){}
var b = new a();
console.log(a.prototype.isPrototypeOf(b)); //true
Copy the code
5. Object.defineProperty()
Hijack the set and GET methods of variables, add attributes to objects, or modify the properties of existing attributes
var a = {};
Object.defineProperty(a, 'name', {
value : 'kong'.enumerable : true // Whether the attribute is enumerable
})
Copy the code
6. Object.getOwnPropertyNames(obj)
Returns an array of the property names of all of the object’s own properties, including properties that are not enumerable but do not include the Symbol value as a name
var obj = { 0: "a".1: "b".2: "c"};
console.log(Object.getOwnPropertyNames(obj).sort()); / / / "0", "1", "2"]
Copy the code
7. Check whether an attribute exists in the object
- Use the in keyword
var person = {
name:'Ming'.age:18
}
console.log('age' in person) // true has its own property
console.log('address' in person) // false
console.log('toString' in person) // true is an inherited property
Copy the code
- Using the object’s hasOwnProperty() method (recommended)
var person = {
name:'Ming'.age:18
}
console.log(person.hasOwnProperty('age')) // true
console.log(person.hasOwnProperty('address')) // false
console.log(person.hasOwnProperty('toString')) // false This is an inherited property, but not its own property
Copy the code
- Use undefined
// Attribute and inherited attribute can be judged
var person = {
name:'Ming'.age:18
}
console.log(person.age ! = =undefined) // true
console.log(person.address ! = =undefined) // false
console.log(person.toString ! = =undefined) // true
Copy the code
There is a problem with undefined. If the value of the attribute is undefined, the method cannot return the desired result, as follows:
var person = {
name:'Ming'.age:undefined
}
console.log(person.age ! = =undefined) // The false attribute exists, but the value is undefined
console.log(person.address ! = =undefined) // false
console.log(person.toString ! = =undefined) // true
Copy the code
4. Check in conditional statements
var person = {}
if(person.age){
person += 1
}
// If age is undefine, NULL,false," ",0, or NaN, it will remain unchanged
Copy the code
8. Check whether the object is empty
- Using Object.keys(obj) in ES6
var data = {};
var arr = Object.keys(data);
console.log(arr.length == 0); // true is null, false is not null
Copy the code
- Convert the JSON object to a JSON string and determine if the string is “{}”
var data = {};
var b = (JSON.stringify(data) == "{}");
console.log(b); //true is null, false is not null
Copy the code
9.
var obj1 = {
name:' '.age:' '.city:'wuhan'.sex:' ',}var obj2 = {
name:' '.city:'nanjing'.sex:'male'
}
Object.keys(obj2).forEach(key= >{
obj1[key] = obj2[key]
})
console.log(obj1,obj2) / / {age: "', city: 'nanjing' name:", "sex:" male "} {city: 'nanjing' name: ", "sex:" male "}
Copy the code