zoukankan      html  css  js  c++  java
  • 浅拷贝与深拷贝区别

    // 浅拷贝
    // 把父对象的属性,全部拷贝给子对象,也能实现继承
    function extendCopy(p) {
    var c = {}
    for(var i in p) {
    c[i] = p[i];
    }
    c.uber = p;
    return c;
    }
    var Doctor = extendCopy(Chinese);
    Doctor.career = "医生";
    // console.log(Doctor.nation);
    // 如果父对象的属性等于数组或另一个对象,实际上,子对象获得的只是一个内存地址,而不是真正的拷贝,因此存在父对象被篡改的可能

    Chinese.birthPlaces = ['北京','上海','香港'];

    var Doctor = extendCopy(Chinese);
    Doctor.birthPlaces.push('厦门');
    console.log(Doctor.birthPlaces)
    console.log(Chinese.birthPlaces)
    // extendCopy()只是拷贝基本类型的数据,我们把这种拷贝叫做"浅拷贝"。这是早期jQuery实现继承的方式。

    // 深拷贝
    function deepCopy(p,c) {
    var c = c || {};
    for(var i in p) {
    // console.log( p[i]);
    if(typeof p[i] === 'object') {
    c[i] = (p[i].constructor === Array) ? [] : {};
    // console.log( c[i]);
    deepCopy(p[i], c[i]);
    }else {
    c[i] = p[i];
    }
    }
    return c;
    }
    var Doctor = deepCopy(Chinese);
    Doctor.birthPlaces.push('厦门');
    // console.log(Doctor.birthPlaces)
    // console.log(Chinese.birthPlaces)
  • 相关阅读:
    期权标的概率密度函数
    Girsanov Theorem
    拉马努金恒等式
    波动率的三类模型
    stack(栈) and heap(堆)
    covar of lognormal variables
    BS 相关的一些近似公式
    布朗运动的一些特殊性质
    排序算法
    Mac node.js
  • 原文地址:https://www.cnblogs.com/neilniu/p/10739547.html
Copyright © 2011-2022 走看看