[es6学习笔记]对象

xiaoxiao2021-02-28  59

对象分类:     ordinary:普通对象     具有js对象所有的默认行为     exotic:特异对象         具有一些与默认行为不符的内部行为     standard:标准对象     es规范中的对象     内建对象:                    所有标准对象都是内建对象 对象字面量语法扩展:     属性初始值的简写:   es5:    function person(name,value){     return {         name:name,         value:value     } } es6: //命名相同的元素可以简写 function person(name,value){     return {         name,         value     } } 对象方法的简写: es5: var  params={     name:'xiaomin',     eat:function (){         return 'aa';     } } es6://有es5全部特征,可拥有super关键字 const params={     name:'xiaomin',     eat(){         return a; //可拥有super关键字     },     see()=>'cc' } 可计算属性名: es6: 可用 [params+"sss"]的形式书写对象名 var puf = 'name'; var person={}; person["last"+puf]="yang"; console.log(person); puf='aa'; console.log(person); Object.is()方法:  用于比较:避免了es5的一些比较问题 //+0 -0 的相等 console.info(+0==-0);//true console.info(+0===-0);//true console.info(Object.is(+0,-0));//false //NaN的不等 console.info(NaN==NaN);//false console.info(NaN==NaN);//false console.info();//true //==的类型转换 console.info(5=='5');//true console.info(Object.is(5,'5'));//false 一般情况下 还是 ===吧 Object.assign()方法: 混合 mixin:      //浅克隆   function mixin(receiver,supplier){     Object.keys(supplier).forEach(function (key){         receiver[key]=supplier[key];     });     return receiver; } //es6版本 var myObj={}; Object.assign(myObj,targetObj.prototype); //并且支持多对象,后面的对象会覆盖前面对象的相同行为 Object.assign(myObj,{type:'name'},{type:'cc',run(){}}); set get函数就不要在混用中使用了 重复属性: es5: 'use strict' var person = {     name:'aa',     name:'cc'//抛出异常 } es6: 'use strict' var person = {     name:'aa',     name:'cc'//不会 } 感觉这是个坑; 自有属性枚举顺序:     es5中没有限制对象中属性的顺序 ,而es6中严格规定了对象的返回顺序;     按照:         数字升序         字符串按加入顺序         所有symbol按加入顺序   var param={     a:'a',     c:'c',     3:2,     1:1,     b:0 } console.info(param);//{ '1': 1, '3': 2, a: 'a', c: 'c', b: 0 } 对于for in 、Object.keys()、JSON.stringify()循环顺序都不是按这个的,可能随机,可能如上 增强对象原型:    es6新增Object.setPrototypeOf()在对象实例化后改变原型:         let person={     getGreeting(){         return 'nihao';     } } let dog = {     getGreeting(){         return "wangwang";     } } let friend=Object.create(person); console.info(friend.getGreeting());//nihao console.info(Object.getPrototypeOf(friend) === person );//true Object.setPrototypeOf(friend,dog); console.info(friend.getGreeting()); //wangwang console.info(Object.getPrototypeOf(friend) === person );//false super: 在setPrototypeOf时 Object.getPrototypeOf(this).getGreeting()调用了父类的getGreeting方法 let person={     getGreeting(){         return 'nihao';     } } let dog = {     getGreeting(){         return "wangwang";     } } let friend= {     getGreeting(){         console.info(Object.getPrototypeOf(this).getGreeting()); //es6中 super.getGreeting()==Object.getPrototypeOf(this).getGreeting.call(this)         return Object.getPrototypeOf(this).getGreeting.call(this)+'hi';     } } Object.setPrototypeOf(friend,person); console.info(friend.getGreeting());//nihaohi Object.setPrototypeOf(friend,dog); console.info(friend.getGreeting());//wangwanghi  用es5方法的问题: let person={     getGreeting(){         return 'nihao';     } } let dog = {     getGreeting(){         return "wangwang";     } } let friend= {     getGreeting(){ //es6中 super.getGreeting()==Object.getPrototypeOf(this).getGreeting.call(this)         return Object.getPrototypeOf(this).getGreeting.call(this)+'hi';     } } Object.setPrototypeOf(friend,person); let relative= Object.create(friend);// 在es5中 因为创建的getGreeting的this指向的relative方法   console.info(person.getGreeting());//nihao console.info(friend.getGreeting());//nihaohi console.info(relative.getGreeting());//error es6中 super就像在方法中先自己创建一个 let 指向父类this一样 正式的方法定义: let params={ //这是方法 getGreeting(){ return 'hello!'; } } //这是个构造,不是方法 function showGreenting(){ return  'hello'; } super引用的方法是 【homeObject】的  而只有方法才在里面;;--
转载请注明原文地址: https://www.6miu.com/read-2628248.html

最新回复(0)