ES6对象操作
1、对象的赋值
ES5语法
let name = 'sun'
let skill = 'web'
let obj = {
name: name,
skill: skill
}
console.log(obj);
![](https://img2018.cnblogs.com/blog/1675843/201905/1675843-20190513083115626-513654805.png)
ES6语法
let name = 'sun'
let skill = 'web'
let obj = {
name,
skill
}
console.log(obj);
![](https://img2018.cnblogs.com/blog/1675843/201905/1675843-20190513083205628-2056415963.png)
结果是相同的,这样会减少代码量
2、key值的构建
let key = 'skill'
let obj = {
[key]: 'web'
}
console.log(obj);
![](https://img2018.cnblogs.com/blog/1675843/201905/1675843-20190513083348855-1909471970.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://img2018.cnblogs.com/blog/1675843/201905/1675843-20190513084017576-724237411.png)