• <noscript id="ggggg"><dd id="ggggg"></dd></noscript>
    <small id="ggggg"></small> <sup id="ggggg"></sup>
    <noscript id="ggggg"><dd id="ggggg"></dd></noscript>
    <tfoot id="ggggg"></tfoot>
  • <nav id="ggggg"><cite id="ggggg"></cite></nav>
    <nav id="ggggg"></nav>
    成人黃色A片免费看三更小说,精品人妻av区波多野结衣,亚洲第一极品精品无码,欧美综合区自拍亚洲综合,久久99青青精品免费观看,中文字幕在线中字日韩 ,亚洲国产精品18久久久久久,黄色在线免费观看

    JavaScript 中對象的深拷貝

    2016-10-29    藍藍設(shè)計的小編

    如果您想訂閱本博客內(nèi)容,每天自動發(fā)到您的郵箱中, 請點這里

    在JavaScript中,對對象進行拷貝的場景比較常見。但是簡單的復制語句只能對對象進行淺拷貝,即復制的是一份引用,而不是它所引用的對象。而更多的時候,我們希望對對象進行深拷貝,避免原始對象被無意修改。

    對象的深拷貝與淺拷貝的區(qū)別如下:

    • 淺拷貝:僅僅復制對象的引用,而不是對象本身;
    • 深拷貝:把復制的對象所引用的全部對象都復制一遍。

    一. 淺拷貝的實現(xiàn)

    淺拷貝的實現(xiàn)方法比較簡單,只要使用是簡單的復制語句即可。

    1.1 方法一:簡單的復制語句

    /* ================ 淺拷貝 ================ */ function simpleClone(initalObj) { var obj = {}; for ( var i in initalObj) {
            obj[i] = initalObj[i];
        } return obj;
    }
    /* ================ 客戶端調(diào)用 ================ */ var obj = {
        a: "hello",
        b: {
            a: "world",
            b: 21 },
        c: ["Bob", "Tom", "Jenny"],
        d: function() {
            alert("hello world");
        }
    } var cloneObj = simpleClone(obj); // 對象拷貝 console.log(cloneObj.b); // {a: "world", b: 21} console.log(cloneObj.c); // ["Bob", "Tom", "Jenny"] console.log(cloneObj.d); // function() { alert("hello world"); } // 修改拷貝后的對象 cloneObj.b.a = "changed";
    cloneObj.c = [1, 2, 3];
    cloneObj.d = function() { alert("changed"); }; console.log(obj.b); // {a: "changed", b: 21} // // 原對象所引用的對象被修改了 console.log(obj.c); // ["Bob", "Tom", "Jenny"] // 原對象所引用的對象未被修改 console.log(obj.d); // function() { alert("hello world"); } // 原對象所引用的函數(shù)未被修改

    1.2 方法二:Object.assign()

    Object.assign() 方法可以把任意多個的源對象自身的可枚舉屬性拷貝給目標對象,然后返回目標對象。但是 Object.assign() 進行的是淺拷貝,拷貝的是對象的屬性的引用,而不是對象本身。

    var obj = { a: {a: "hello", b: 21} }; var initalObj = Object.assign({}, obj);
    
    initalObj.a.a = "changed"; console.log(obj.a.a); // "changed"

    二. 深拷貝的實現(xiàn)

    要實現(xiàn)深拷貝有很多辦法,有最簡單的 JSON.parse() 方法,也有常用的遞歸拷貝方法,和ES5中的 Object.create() 方法。

    2.1 方法一:使用 JSON.parse() 方法

    要實現(xiàn)深拷貝有很多辦法,比如最簡單的辦法是使用 JSON.parse()

    /* ================ 深拷貝 ================ */ function deepClone(initalObj) { var obj = {}; try {
            obj = JSON.parse(JSON.stringify(initalObj));
        } return obj;
    }
    /* ================ 客戶端調(diào)用 ================ */ var obj = {
        a: {
            a: "world",
            b: 21 }
    } var cloneObj = deepClone(obj);
    cloneObj.a.a = "changed"; console.log(obj.a.a); // "world"

    這種方法簡單易用。

    但是這種方法也有不少壞處,譬如它會拋棄對象的constructor。也就是深拷貝之后,不管這個對象原來的構(gòu)造函數(shù)是什么,在深拷貝之后都會變成Object。

    這種方法能正確處理的對象只有 Number, String, Boolean, Array, 扁平對象,即那些能夠被 json 直接表示的數(shù)據(jù)結(jié)構(gòu)。RegExp對象是無法通過這種方式深拷貝。

    2.2 方法二:遞歸拷貝

    代碼如下:

    /* ================ 深拷貝 ================ */ function deepClone(initalObj, finalObj) { var obj = finalObj || {}; for (var i in initalObj) { if (typeof initalObj[i] === 'object') {
                obj[i] = (initalObj[i].constructor === Array) ? [] : {}; arguments.callee(initalObj[i], obj[i]);
            } else {
                obj[i] = initalObj[i];
            }
        } return obj;
    }

    上述代碼確實可以實現(xiàn)深拷貝。但是當遇到兩個互相引用的對象,會出現(xiàn)死循環(huán)的情況。

    為了避免相互引用的對象導致死循環(huán)的情況,則應(yīng)該在遍歷的時候判斷是否相互引用對象,如果是則退出循環(huán)。

    改進版代碼如下:

    /* ================ 深拷貝 ================ */ function deepClone(initalObj, finalObj) { var obj = finalObj || {}; for (var i in initalObj) { var prop = initalObj[i]; // 避免相互引用對象導致死循環(huán),如initalObj.a = initalObj的情況 if(prop === obj) { continue;
            } if (typeof prop === 'object') {
                obj[i] = (prop.constructor === Array) ? [] : {}; arguments.callee(prop, obj[i]);
            } else {
                obj[i] = prop;
            }
        } return obj;
    }

    2.3 方法三:使用Object.create()方法

    直接使用var newObj = Object.create(oldObj),可以達到深拷貝的效果。

    /* ================ 深拷貝 ================ */ function deepClone(initalObj, finalObj) { var obj = finalObj || {}; for (var i in initalObj) { var prop = initalObj[i]; // 避免相互引用對象導致死循環(huán),如initalObj.a = initalObj的情況 if(prop === obj) { continue;
            } if (typeof prop === 'object') {
                obj[i] = (prop.constructor === Array) ? [] : Object.create(prop);
            } else {
                obj[i] = prop;
            }
        } return obj;
    }

    三. 參考:jQuery.extend()方法的實現(xiàn)

    jQuery.js的jQuery.extend()也實現(xiàn)了對象的深拷貝。下面將官方代碼貼出來,以供參考。

    官方鏈接地址:https://github.com/jquery/jquery/blob/master/src/core.js。

    jQuery.extend = jQuery.fn.extend = function() { var options, name, src, copy, copyIsArray, clone,
            target = arguments[ 0 ] || {},
            i = 1,
            length = arguments.length,
            deep = false; // Handle a deep copy situation if ( typeof target === "boolean" ) {
            deep = target; // Skip the boolean and the target target = arguments[ i ] || {};
            i++;
        } // Handle case when target is a string or something (possible in deep copy) if ( typeof target !== "object" && !jQuery.isFunction( target ) ) {
            target = {};
        } // Extend jQuery itself if only one argument is passed if ( i === length ) {
            target = this;
            i--;
        } for ( ; i < length; i++ ) { // Only deal with non-null/undefined values if ( ( options = arguments[ i ] ) != null ) { // Extend the base object for ( name in options ) {
                    src = target[ name ];
                    copy = options[ name ]; // Prevent never-ending loop if ( target === copy ) { continue;
                    } // Recurse if we're merging plain objects or arrays if ( deep && copy && ( jQuery.isPlainObject( copy ) ||
                        ( copyIsArray = jQuery.isArray( copy ) ) ) ) { if ( copyIsArray ) {
                            copyIsArray = false;
                            clone = src && jQuery.isArray( src ) ? src : [];
    
                        } else {
                            clone = src && jQuery.isPlainObject( src ) ? src : {};
                        } // Never move original objects, clone them target[ name ] = jQuery.extend( deep, clone, copy ); // Don't bring in undefined values } else if ( copy !== undefined ) {
                        target[ name ] = copy;
                    }
                }
            }
        } // Return the modified object return target;
    };

     

     藍藍設(shè)計www.lzhte.cn )是一家專注而深入的界面設(shè)計公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設(shè)計、BS界面設(shè)計  cs界面設(shè)計 、 ipad界面設(shè)計 、 包裝設(shè)計 、 圖標定制 、 用戶體驗 、交互設(shè)計、 網(wǎng)站建設(shè) 平面設(shè)計服務(wù) 

     

    日歷

    鏈接

    個人資料

    藍藍設(shè)計的小編 http://www.lzhte.cn

    存檔

    主站蜘蛛池模板: 国产小视频在线观看| 美女一区二区在线观看| 99久久综合国产精品免费| 免费人成网站在线观看不卡| 成人国产三级在线播放| 播放不卡的日本作爱a视频| 亚洲第一区精品日韩在线播放| 精品乱人伦一区二区三区 | 美女视频黄又黄又免费高清| 成码无人AV片在线电影网站| 无码av无码免费一区二区| 少妇被粗大的猛烈进出免费视频| 国产精品三级一二三区 | 好男人WWW社区视频在线资源| 精品熟女日韩中文十区| 国产午夜精品成人在线| 国产精品麻豆中文字幕| 亚洲av成人专区精品| 亚洲男人AV天堂午夜在| 欧美日韩中文国产va另类| 丰镇市| 香港三日本三级少妇三级视频| 2021最新精品国自产拍视频| 精精国产xxx在线视频app| 松原市| 国产午夜在线观看视频| 精品人妻一区二区三区中文久久| 国产成人高清精品免费软件| 一区二区福利在线视频| 亚洲中文在线看视频一区| 国产精品午夜福利麻豆| 久热最新精品在线视频| 精品亚洲欧美高清不卡高清| 日本午夜爱爱一区二区| 中文字幕久久精品人妻| 六月丁香五月| XXX国产| 国产亚洲精品第一综合| 秋霞无码久久久精品| 亚洲国产午夜精品福利| 德兴市|