ES6-對象賦值,key值得構建,is()方法對比對象,assign()合併對象 ...
ES6對象賦值:
// es5 let name = "ananiah"; let skill ='web'; let obj = {name:name,skill:skill}; console.log(obj) // es6 用變數形式賦值 let name = "ananiah"; let skill ='web'; let obj = {name,skill}; console.log(obj)
key值構建
// es6-key值的構建 let key = "skill"; var obj ={ [key]:'web' } console.log(obj)
es5自定義對象方法
//自定義對象的方法(es5) let objobj = { add:function(a,b){ return a+b; } } console.log(objobj.add(2,3)) //5
is() 比較兩個對象
//is() 用來比較兩個對象 let obja = {name:'annaiah'}; let objb = {name:'annaiah'}; console.log(obja.name===objb.name) //true es5方法 console.log(Object.is(obja.name,objb.name)) //true //上面兩個方法的區別: === 同值相等 is嚴格相等 console.log(+0 === -0); //true console.log(NaN === NaN); //false console.log(Object.is(+0,-0)); //false console.log(Object.is(Nan,NaN)); //trueassign 合併對象
//assign 合併對象 let a = {a:'annaiah'}; let b = {b:'大誒啊'}; let c = {c:'web'}; let d = Object.assign(a,b,c); console.log(d)