zoukankan      html  css  js  c++  java
  • ES6 对象的创建及操作

    对象的创建方式包括 以下3种:(new Object(),new 构造函数的形式 , Object.create(properties),)

    "use strict"

    // 方式1

    var person = new Object()

    person["name"]="jimmy"

    console.log(person.name)

    person.age=20

    console.log(person.age)

    console.log(person)

     var myCar = new Object()

    var propertyName = "make";

    myCar[propertyName] = "Ford";

    var propertyName1 = "year"

    myCar[propertyName1]=1990

    console.log(myCar.make)

    console.log(myCar.year)

    // 方式2

    //构造函数

    function Car(){

    this.make = "Ford"

    this.model = "F123"

    }

    var obj = new Car()

    console.log(obj.make)

    console.log(obj.model)

    // 方式3 Object.create

    var roles = {

    type:"Admin",//Default value of properties

    displayType:function(){

      console.log(this.type);//Mehtod which will display type of role

    }

    }

    var super_role = Object.create(roles);

    super_role.displayType();

    var guest_role = Object.create(roles);

    guest_role.type = "Guest";

    guest_role.displayType();

     var testObj = Object.create(null)

    testObj.type = "xx"

    testObj.displayType = new Function("a","console.log(a)");

    testObj.displayType("abc")

    //object assign

    var det = {name:"Tom",ID:"E1001"};

    // 初始化对象并且给默认值

    var copy = Object.assign({},det);

    console.log(copy);

    for(let val in copy){

    console.log(copy[val]);

    }

    //

    var properties = {

    name: "john",

    ID :"E1002",

    age: 28

    };

    var jo = Object.create(properties);

    console.log(jo.name);

    console.log(jo.ID);

    var john = Object.assign(properties);

    console.log(john);

    var john1 = Object.assign(john,det);

    console.log(john1);

    //合并对象,合并对象时大对象没有做拷贝的操作,而是直接引用了对象的指针

    var o1 = {a:10};

    var o2 = {b:20};

    var o3 = {c:30};

    // 将值合并到o1 并且返回o1 , 返回值就是被合并的对象

    var obj = Object.assign(o1,o2,o3);

    console.log(obj);

    console.log(o1);

    if(o1 === obj){

    console.log("o1 is the same as the obj");

    }else{

    console.log("o1 is different from obj");

    }

    // o2的值没有改变

    console.log(o2);

     var o1 = {a:10};

    var obj = Object.assign(o1);

    obj.a++;

    console.log("Value of 'a' in the Merged object after increment");

    console.log(obj.a);

    console.log("Value of 'a' in the Original Object after increment");

    console.log(o1.a);

     // delete properties 删除对象中的属性

     var myobj = new Object;

    myobj.a = 5;

    myobj.b = 12;

     console.log("before deleting the myobj value is :");

    console.log(myobj);

    delete myobj.a

    console.log("after deleting the myobj value is :");

    console.log(myobj);

     //比较

    var val1 = {name:"Tom"};

    var val2 = {name:"Tom"};

    console.log(val1 == val2)//return false

    console.log(val1 === val2)//return false

    var val1 = {name:"Tom"};

    var val2 = val1;

    console.log(val1 == val2);

    console.log(val1 === val2);

    var emp = {name:'John',Id:3}

    var {name,Id} = emp

    console.log(name)

    console.log(Id)

  • 相关阅读:
    移动应用滑动屏幕方向判断解决方案,JS判断手势方向
    M1808本地配置IP全过程
    嵌入式板子tftpd交叉编译及使用
    七牛云ARM64交叉编译流程
    记录M1808Ubuntu18.04固定IP配置方案
    so库移植记录
    c++ 动态库的编译
    Linux提示Syntax error: end of file unexpected (expecting "then") 提示错误
    ubuntu18.04设置静态ip
    WebFlux中thymeleaf视图找不到的问题解决
  • 原文地址:https://www.cnblogs.com/codetime/p/7381798.html
Copyright © 2011-2022 走看看