zoukankan      html  css  js  c++  java
  • 非构造函数的继承和拷贝

    、object()方法(不够完善)

    只能用于对象

    
    
      var Chinese = {
        nation:'中国'
      };
      var Doctor ={
        career:'医生'
      };
      function object(parent) { 
        function F() {}
        F.prototype = parent;
        return new F();
      }
        Doctor = object(Chinese);
        console.log(Doctor.nation); //中国

    二、浅拷贝(不够完善)

    对象的浅拷贝:

    
    
     var Chinese = {
        nation:'中国',
           birthPlaces:['北京','上海','香港']
      };
      var Doctor ={
        career:'医生'
      };
       function extendCopy(p) {
        var c = {};
        for (var i in p) { //子对象获得的只是一个内存地址,而不是内容
          c[i] = p[i];
        }
        //c.uber = p;
        return c;
      }
        Doctor = extendCopy(Chinese);
        console.log(Doctor.nation); //中国
        Doctor.birthPlaces.push('厦门');
        console.log(Doctor.birthPlaces); //北京, 上海, 香港, 厦门
        console.log(Chinese.birthPlaces); //北京, 上海, 香港, 厦门
        //父对象也被篡改了。第一点的object()方法也同样存在这个问题。也可以看成对象的浅拷贝。

    数组的浅拷贝:

    
    
    //1
    var arr2 = arr1.slice();
    //2
    var arr2 = arr1.concat();

    通用浅拷贝函数:

    
    
    var simpleCopy = function(o){
        if (o instanceof Array) { //数组浅拷贝
            var n = [];
            for (var i = 0i < o.length; ++i) { //注意是++i,先加再执行
                n[i] = o[i];
            };
            return n;
        } else if (o instanceof Object) { //对象浅拷贝
            var n = {};
            for (var i in o) { //对象遍历要用for in
                n[i] = o[i];
            };
            return n;
        }
    }

     注意直接用等号并不是浅拷贝,而是相等,修改任意一项都会有影响。

    、深拷贝

    对象的深拷贝:

    
    
     var Chinese = {
        nation:'中国',
           birthPlaces:['北京','上海','香港']
      };
      var Doctor ={
        career:'医生'
      };
       function deepCopy(pc) {
        var c = c || {}; //这里决定了返回的是对象
        for (var i in p) {
          if (typeof p[i] === 'object') {
            c[i] = (p[i].constructor === Array) ? [] : {};
            deepCopy(p[i], c[i]);
          } else {
             c[i] = p[i];
          }
        }
        return c;
      }
        Doctor = deepCopy(Chinese);
        console.log(Doctor.nation); //中国
        Doctor.birthPlaces.push('厦门');
        console.log(Doctor.birthPlaces); //北京, 上海, 香港, 厦门
        console.log(Chinese.birthPlaces); //北京, 上海, 香港

    对象的深拷贝:(最简单)

    
    
    var obj2 = JSON.parse(JSON.stringify(obj));

    通用深拷贝函数:

    
    
    var deepCopy = function(o) {
        if (o instanceof Array) { //一定要把Array判断放在Object判断上面
            var n = [];
            for (var i = 0i < o.length; ++i) { 
                n[i] = deepCopy(o[i]);
            };
            return n;

        } else if (o instanceof Object) {
            var n = {};
            for (var i in o) {
                n[i] = deepCopy(o[i]);
            };
            return n;
        } else { //返回o
            return o;
        }
    }

     

  • 相关阅读:
    LintCode Python 简单级题目 488.快乐数
    LintCode Python 简单级题目 100.删除排序数组中的重复数字 101.删除排序数组中的重复数字II
    LintCode Python 简单级题目 373.奇偶分割数组
    LintCode Python 简单级题目 39.恢复旋转排序数组
    LintCode Python 简单级题目 35.翻转链表
    LintCode Python 简单级题目 451.两两交换链表中的节点
    LintCode Python 简单级题目 174.删除链表中倒数第n个节点
    aws查看官方centos镜像imageid
    linux shell脚本查找重复行/查找非重复行/去除重复行/重复行统计
    php配置优化-生产环境应用版
  • 原文地址:https://www.cnblogs.com/shen076/p/6558835.html
Copyright © 2011-2022 走看看