1 属性简洁表示法 && 属性名表达式
let name = 'kiw';
let age = '18';
let s = 'school';
let obj = {
name,
age,
[s]: 'imooc',
study() {
console.log(this.name + '正在学习');
}
}
2 Object.is()
console.log(Object.is(2, '2')) //false
console.log(Object.is(NaN, NaN)) //true 可以判断NaN
console.log(Object.is(+0, -0)) //false
let obj1 = {
name: 'kiw',
age: 18,
}
let obj2 = {
name: 'kiw',
age: 18,
}
console.log(Object.is(obj1, obj2)) //false 引用类型 相当于比较两个地址 所以结果为false
3 扩展运算符 与 Object.assign()
let x = {
a: 3,
b: 4
}
let y = {
c: 5,
a: 6
}
Object.assign(y, x); //合并两个对象 相同属性后面覆盖前面
console.log(y);
4 in
let x = {
a: 3,
b: 4
}
console.log('a' in x); //true 判断x对象中是否有a属性
console.log('d' in x); //false
5 对象的遍历方式
//for in
let obj = {
name: 'kiw',
age: '18',
school: 'imooc',
}
for (const key in obj) {
console.log(key, obj[key])
}
let obj = {
name: 'kiw',
age: '18',
school: 'imooc',
};
Object.keys(obj).forEach(key => {
console.log(key, obj[key])
})
Object.getOwnPropertyNames(obj).forEach(key => {
console.log(key, obj[key])
})
网友评论