1、对象的赋值
ES5语法
let name = 'sun'
let skill = 'web'
let obj = {
name: name,
skill: skill
}
console.log(obj);
![](https://i-blog.csdnimg.cn/blog_migrate/5007149d0c2446db452bd43b20542768.png)
ES6语法
let name = 'sun'
let skill = 'web'
let obj = {
name,
skill
}
console.log(obj);
![](https://i-blog.csdnimg.cn/blog_migrate/92ec54d9893055bcaf64011c5afa5471.png)
结果是相同的,这样会减少代码量
2、key值的构建
let key = 'skill'
let obj = {
[key]: 'web'
}
console.log(obj);
![](https://i-blog.csdnimg.cn/blog_migrate/26fd4d3bcb95820e5158ac01c1fbeb2c.png)
3、自定义对象的方法
let obj={
add:function(a,b){
return a+b
}
}
console.log(obj.add(1,2)); //3
4、is()方法
let obj1 = {
name: 'js'
}
let obj2 = {
name: 'js'
}
console.log(obj1.name === obj2.name); //true
console.log(Object.is(obj1.name,obj2.name)); //true
===与is()的区别:===通值相等,is严格相等
console.log(+0 === -0); //true
console.log(NaN === NaN); //false
console.log(Object.is(+0, -0)); //false
console.log(Object.is(NaN, NaN)); //true
5、assign 合并对象
let a = {
a: 'js'
}
let b = {
b: 'sun'
}
let c = {
c: 'web'
}
let d = Object.assign(a, b, c)
console.log(d);
![](https://i-blog.csdnimg.cn/blog_migrate/5c6c4c6f1fe0ee2859efc78263798815.png)