自定义对象实例之间的相同性与合并
·
Yin灏
判断两个对象实例的数据是否完全相同
console.log(Object.is('mars', 'Mars')); // false
console.log(Object.is('Mar' + 's', 'Mars')); // true
console.log(Object.is(document.body, document.body)); // true
console.log(Object.is([], [])); // false
console.log(Object.is(['good', 'day'], ['good', 'day'])); // false
var profile01 = { name: 'Jasper' };
var profile02 = { name: 'Jasper' }
console.log(Object.is(profile01, profile02)); // false
console.log(Object.is(null, null)); // true
console.log(Object.is(null, undefined)); // false
console.log(Object.is(undefined, undefined)); // true
console.log(Object.is(NaN, 0 % 0)); // true
console.log(Object.is(0, -0)); // false
console.log(Object.is(-0, -0)); // true
合并多个对象实例的所有成员
通过内置函数 Object.assign()
可以将过个对象实例的所有成员(属性和函数),合并至特定的对象中。
var profile = {
name: 'Sean',
gender: 'male',
age: 25
};
var job = {
department: 'IT',
position: 'software engineer',
mothly_salary: '10000'
};
var info = {
log_time: new Date().toLocaleString()
}
// 将 对象实例 profile 和 job 合并到 info 对象实例中
Object.assign(info, profile, job);
console.log(info);
/*
{
"log_time": "2021/6/24 下午2:37:46",
"name": "Sean",
"gender": "male",
"age": 25,
"department": "IT",
"position": "software engineer",
"mothly_salary": "10000"
}
*/