1、对象的赋值
ES5语法
let name = 'sun' let skill = 'web' let obj = { name: name, skill: skill } console.log(obj);
ES6语法
let name = 'sun' let skill = 'web' let obj = { name, skill } console.log(obj);
结果是相同的,这样会减少代码量
2、key值的构建 let key = 'skill' let obj = { [key]: 'web' } console.log(obj);
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);