• ES6 assign




    <!DOCTYPE html>
    <html >
    <head>
    <meta charset="UTF-8">   
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />       
    <title>ES6 assign</title>
    <style type="text/css">
    html {height: 100%;}
    body { 200px;height: 100%;position: relative;padding: 0;margin: 0;}
    </style>
    </head>
    <body>
    <script src="https://cdn.bootcss.com/jquery/1.9.0/jquery.js"></script>
    <script>
        var name = "zhangsan";
        var password = "1111111";
        var obj = {
           name,
           password,
           arr: [1, 2, 3, 4],
           sayName() {
              console.log(this.name);
           }
        };
        var obj1 = {
           age: 20,
           name: 'xutongbao'
        };
    
        //花括号叫目标对象,后面的obj、obj1是源对象。对象合并是指:将源对象里面的属性添加到目标对象中去,若两者的属性名有冲突,后面的将会覆盖前面的
        var nObj = Object.assign({}, obj, obj1); 
        console.log(nObj);  //{name: "xutongbao", password: "1111111", arr: Array(4), sayName: ƒ, age: 20}
        nObj.arr.push(5);
        console.log(obj.arr); //[1, 2, 3, 4, 5]  此处说明assign是浅拷贝
    
        var arr11 = Object.assign([1,2,3], [4,5]);
        console.log(arr11); //[4,5,3]  对象是根据属性名来对应,数组是根据索引号来对应
    </script>
    </body>
    </html>

  • 相关阅读:
    request、bs4爬虫
    1031 查验身份证
    1029 旧键盘
    1028 人口普查
    1027 打印沙漏
    1026 程序运行时间
    1025 反转链表
    1024 科学计数法
    1022 D进制的A+B
    1021 个位数统计
  • 原文地址:https://www.cnblogs.com/xutongbao/p/9924844.html
Copyright © 2020-2023  润新知