ES6入门---第二单元 模块三:对象新增、

一:对象简洁语法:

1、变量简洁

<script>
        let name = 'Strive';
        let age = 18;

        let json ={
            name,   //name:name,
            age     //age:age
        };

        console.log(json);

    </script>

2、函数简洁

let json ={
            name,   //name:name,
            age,     //age:age
            /* showA:function(){
                return this.name;
            } */
            showA(){//不要用箭头函数
                return this.name;
            },
            showB(){
                return this.age;
            }
        };

3、有关解构

   let x = 10;
        let y =20;
        function show({x,y}){
            console.log(x, y);
        }

        show({x,y})

二、对象新增

1、

Object.is():    用来比较两个值是否相等

    Object.is('a','a');

补充:JavaScript NaN 属性

NaN 属性是代表非数字值的特殊值。该属性用于指示某个值不是数字。可以把 Number 对象设置为该值,来指示其不是数字值。

提示:请使用 isNaN() 全局函数来判断一个值是否是 NaN 值。

 Object.is(+0, -0); false

Object.is(NaN, NaN); false

2、Object.assign(): 

用途:
        1. 复制一个对象
        2. 合并参数

let 新的对象 = Object.assign(目标对象, source1, srouce2....)

原则:后覆盖前

例:

 let json = {a:1};
        let json2 = {b:2, a:2};
        let json3 = {c:3};

        let obj = Object.assign({}, json, json2,json3);

        console.log(obj);

3、补充:


ES2017引入:
    Object.keys()
    Object.entries();
    Object.values();

  let {keys, values, entries} = Object;

        let json = {
            a:1,
            b:2,
            c:3
        };

        for(let key of keys(json)){
            console.log(key);
        }

        for(let value of values(json)){
            console.log(value);
        }

        for(let item of entries(json)){
            console.log(item);
        }

        for(let [key, val] of entries(json)){
            console.log(key, val);
        }

4、【ES2018】对象扩展运算符:...

 let json = {a:3, b:4};

        let json2 = {...json};

        delete json2.b;
        console.log(json2);
        console.log(json);

猜你喜欢

转载自blog.csdn.net/qq_41775119/article/details/81714172
今日推荐