es6 对象的扩展

一、现在还有很多浏览器不能直接使用es6语法。特别是手机端的一些低版本的浏览器。都需要用bale转换一下。

但是目前流行的框架中(vue,react,angular)。都有自己的脚手架,都能用webpack转换下。或者直接自己配置webpack , fis3,nowa 等转换。

照样不是美滋滋。

二、属性的简洁写法 

//1.属性简洁表示语法
        var foo = 'bar';
        var obj = {foo};
        console.log(obj);
        //创建对象的函数
        function createOjb(x = 1,y = 1){  //x = 1, y = 1; 参数的默认值
            return {
                x,y
            }
        }
        var newObj = createOjb(); 
        console.log(newObj); //{x:1,y:1} 
        var birthDate = '2017/8/12'
        //2 方法的简写
        var person = {
            name:'绿巨人',
            age:'200岁',
            birthDate,
            say(){
                console.log(this.name);  //等同于  say:function(){ console.log(this.name)};
            }
        }
        person.say();  // 绿巨人
        //in  方法
         var msg = {
            hello:'helloValue',
            world:'worldValue'
         }
         console.log('hello' in msg,'helloValue' in msg); 
         // true,false; => 判断某个键值是在某个对象里面
         //commonJS 模块化输出
         function Obj(methods){
            this.methods = methods || {};
         }
         Obj.prototype.getItem = function(key){
            return key in this.methods ? methods[key] : null;
         }
         Obj.prototype.setItem = function(key,value){
            this.methods[key] = value;
         }
         var obj = new Obj();
         //module.exports = {obj};
         //4.注意点  :简洁写法的属性名总是字符串,这会导致一些看上去比较奇怪的结果。

三、属性表达式

//属性名表达式
         // 1. 对象添加属性的两种方式
         var newObj = new Object();
         newObj.name = 'html';
         newObj['age'] = '20岁';
         //对象字面量的方式  se5 中字面量方式下 属性名字只能用 字符串形式。不能用 ['name']
         var newObj1 = {
            name:'css',
            age:'30岁'
         }
         //SE6
         var newObj2 = {
            ['name']:'js',
            ['a' + 'ge']:'40岁',
            ['hello world']:'say hello world',
            ['say' + ' hi'](){
                console.log(this['hello world'])
            }
         }
         console.log(newObj2.name); // jss
         console.log(newObj2['hello world']);  // say hello world
         newObj2['say hi'](); // say hello world
         //!!!注意  属性名表达式是不能喝属性简写一起使用的
         var objKey = {a:1};
         var newObj3 = {
            [objKey]:'我是一个对象'
         }
         console.log(newObj3);  // {[object object]:'我是一对象'}
         console.log(newObj3[{a:1}]); // 我是一个对象  
         console.log(newObj3['object object']); // undefined  是不是很奇怪啊

 四、Object.is()

//Object.is();
         //1.es5中
         console.log(+0 === -0); // true
         console.log(NaN === NaN);  //false
         //2.es6中
        console.log(Object.is(+0,-0)); //false
        console.log(Object.is(NaN,NaN)); //true
        //在ES5中部署Object.is(); 方法
        Object.defineProperty(Object,'is',{
            value(x,y){
                if(x === y){
                    return x !== 0 || 1/x === 1/y;
                }
                //针对NaN的情况
                return x !== x && y !== y;
            },
            configurable:true,
            enumerable:false,
            writable:true
        })

五、Object.assign()

 //Object.assign();  对象的合并  熟悉jquery 的人 var defaults = {a : 1} $.extend({},{},defaults || //{});
        var target = {name:'es6'};
        var target1 = {age:'5年'};
        var target2 = {name:'es7'};
        var target3 = {sex:'body'};
        var defaults = {};
        Object.assign(defaults,target,target1,target2,target3);
        console.log(defaults);  //  {name: "es7", age: "5年", sex: "body"}
        //从上面的defaults  的值可以看出 后面的属性会覆盖前面的属性。跟$.extend({} ,defaults || {});  
        // 一个道理
        defaults.name = 'es8';
        console.log(defaults); //{name: "es8", age: "5年", sex: "body"}
        console.log(target2); //{ name:"es7"}
        /*
            !!! 注意 :: =>  由此可以看出 Object.assign();  是一个深拷贝的方法
            !!!!!! (前提是目标某个原对象中没有对象属性)
            ex:target = {
                family:{
                    child:'七七'
                }
            }
            目标对象属于对象属性时。就是浅拷贝啦。 跟 $.extend(true,{} , defaults || {}); 方法有区别。加了true 对象属性也进行深拷贝
        */
            var tar = {
                family:{
                    child:'七七'
                }
            }
            var defaults1 = {

            };
            Object.assign(defaults1,tar);
            console.log(defaults1); // { family:{child:"七七"}};
            defaults1.family.child = "琪琪";
            console.log(defaults1); // { family:{child:"琪琪"}};
            console.log(tar); // { family:{child:"琪琪"}};
//注意点2
            // :: !!! Ojbect.assign() 方法只能克隆原对象。不能够克隆其继承的属性。
            function cloneObject(obj){
                  let originProto = Object.getPrototypeOf(obj);
                    return Object.assign(Object.create(originProto), obj); 
            }
            // Object.create()  方法是对其原型

六、属性的可枚举性

 //对象的可枚举性
            var objEn = {};
            Object.defineProperty(objEn,'foo',{
                value:'不可枚举',
                configurable:true,
                enumerable:false,
            }) 
            console.log(Object.getOwnPropertyDescriptor(objEn,'foo'));
            //{value: "不可枚举", writable: false, enumerable: false, configurable: true}
    /*
        描述对象的enumerable属性,称为”可枚举性“,如果该属性为false,就表示某些操作会忽略当前属性。
        ES5 有三个操作会忽略enumerable为false的属性。
        for...in循环:只遍历对象自身的和继承的可枚举的属性
        Object.keys():返回对象自身的所有可枚举的属性的键名
        JSON.stringify():只串行化对象自身的可枚举的属性
        ES6 新增了一个操作Object.assign(),会忽略enumerable为false的属性,只拷贝对象自身的可枚举的属性。
       */

七、属性的遍历 

//属性的遍历
        var ojbEach = {
            name:'es8',
            age:'100年'
        }
        Object.defineProperty(ojbEach,'bar',{
            value:'不可枚举',
            enumerable:false,
            writable:true,
            configurable:true
        })
        //方法1 for  in 循环遍历对象自身的和继承的可枚举属性(不含 Symbol 属性)。
        for(var key in ojbEach){
            console.log(ojbEach[key]); // es8 100年
        }
        //方法2  Object.keys(); 返回一个数组,包括对象自身的(不含继承的)所有可枚举属性(不含 Symbol 属性)。
        var objArr = Object.keys(ojbEach);  //
        console.log(objArr); // ["name", "age"]
        /*
        方法3 Object.getOwnPropertyNames 返回一个数组,包含对象自身的所有属性(不含 Symbol 属性,但是包括不可枚举属性)。
        */
        var objEnArr = Object.getOwnPropertyNames(ojbEach);//
        console.log(objEnArr); //["name", "age", "bar"]
        //方法4 Object.getOwnPropertySymbols(obj) 返回一个数组,包含对象自身的所有 Symbol 属性。
      /*方法5 Reflect.ownKeys返回一个数组,包含对象自身的所有属性,不管属性名是 Symbol 或字符串,也不管是否可枚举。
    */
原文地址:https://www.cnblogs.com/createGod/p/7351214.html