//ES6允许把声明的变量直接赋值给对象 let name="jspang"; let skill= 'web'; var obj= {name,skill}; console.log(obj);//Object {name: "jspang", skill: "web"} //有时候需要在后台去除key值,而不是前台定义好的,就要重新构建key值,把取的key,用[]进行对象的构建 let key="skill"; var obj={ [key]:'web2' } console.log(obj.skill)//web2 //自定义对象方法 var obj2={ add:function(a,b) { return a+b } } console.log(obj2.add(1,2))//3 //Object.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 //===同值相等,is()严格相等 //Object.assign合并对象 var a={a:'jspang3'}; var b={b:'技术胖3'}; console.log(Object.assign(a,b))
//Set和WeakSet数据结构 //Set很像数组,但不是数组,set不允许内部有重复的值,可以进行循环输出 let setArr = new Set(['jspang','技术胖','web','jspang']); //增删查 add,delete(clear),has //size可以获得set的数量 let weakObj=new WeakSet(); let obj={a:'jspang',b:'技术胖'} weakObj.add(obj); console.log(weakObj); //如果你直接在new 的时候就放入值,将报错,不允许重复
//Symbol在对象中的应用 var jspang = Symbol(); var obj={ [jspang]:'技术胖' } console.log(obj[jspang]); obj[jspang]='web'; console.log(obj[jspang]); //Symbol对象元素的保护作用 let obj={name:'jspang',skill:'web'}; let age=Symbol(); obj[age]=18; for (let item in obj){ console.log(obj[item]);//jspang web } console.log(obj);//{name: "jspang", skill: "web", Symbol(): 18}
//map数据结构
//增删查 set delete has
//取get 清楚clear 获取长度size