对象的扩展
1.对象的简洁表示
2.属性表达式
3.扩展运算符
4.super关键字
5.Object新增方法
1.对象的简洁表示 es5 var name = "name"; var age = 18; var person_es5 = { name:name, age:age, //定义方法 intro:function(){ console.log("my name is:"+name,"I am:"+age+"years old") } }; console.log(person_es5) person_es5.intro(); es6 const person_es6 = { name, age, //定义方法简写 intro(){ console.log("my name is:"+name,"I am:"+age+"years old") } } console.log(person_es6); 2.属性表达式 es5 var filedName = "sex"; var obj = { //属性名是直接变成字符串,不会当作引用去查找值 filedName:18 } console.log(obj);//filedName:18 es6 const obj_es6 = { [filedName]:18 } console.log(obj);//sex:18 3.扩展运算符... let {a,b,d} = {a:'hello',b:'world',c:'mickey',d:'tom',e:'minnie'}; console.log(a,b,d)//hello world tom let {a,b,...d} = {a:'hello',b:'world',c:'mickey',d:'tom',e:'minnie'}; console.log(a,b,d)//hello world {c:'mickey',d:'tom',e:'minnie'} 4.super关键字:指向当前对象的prototype对象 const pro = { name:'tom' }; const obj = { name:'mickey', getName(){ console.log(this.name); //mickey console.log(super.name);//tom } }; //将pro设置为obj的原型对象;即设置obj的原型为pro Object.setPrototypeOf(obj,pro) obj.getName();//tom 5.Object新增方法 a.Object(obj1,obj2):相当于=== b.Object.assign(obj1,obj2):浅拷贝 c.Object.setPrototypeOf(obj1,obj2) Object.getPrototypeOf(obj) d.Object.entries(obj) Object.keys(obj) Object.values(obj) let res = Object.is("tom","tom"); console.log(res) //true console.log("tom"==="tom"); //true let obj = {},obj2 = obj; console.log(Object.is(obj,obj2)) //true let obj1 = {name:"tom"}; let obj2 = {age:19}; Object.assign(obj1,obj2); console.log(obj1,obj2) //obj1:{name:"tom",age:19} obj2:{age:19} let obj = { name:"tom", age:18, sex:"man" } for(let key of Object.key(obj)){ console.log(key); } for(let value of Object.values(obj)){ console.log(value); } for(let [key,value] of Object.entries(obj)){ console.log(key,value); }