zoukankan      html  css  js  c++  java
  • 对象的扩展

    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);
    }
  • 相关阅读:
    socket用法以及tomcat静态动态页面的加载
    SQL2000的三种“故障还原模型”
    杀毒软件拦截的,看不懂,留作纪念
    TOMCAT如何建立两个端口或服务
    非正常关机后造成数据库 置疑 状态的解决办法
    各数据表的空间使用量
    SQL Server 2008维护计划 出错 无法实现自动备份
    SQL2008安装自动退出
    oracle自动备份
    吐槽一下金山卫士
  • 原文地址:https://www.cnblogs.com/lisa2544/p/15560777.html
Copyright © 2011-2022 走看看