JavaScript, jquery deep copy, shallow copy __javascript

Source: Internet
Author: User

no copy of the situation

var obj = {
    a:10
};
var obj2 = obj;
obj2.a =;
alert (OBJ.A); ==>  //assignment will directly modify the value of OBJ.A

deep copy and shallow copy of JavaScript

A shallow copy of JavaScript
Shallow copies replicate only the properties of an object in turn, and do not replicate recursively, and JavaScript storage objects are stored addresses, so shallow replication causes obj.arr and Shallowobj.arr to point to the same memory address

var obj = {
    a:10
}
function copy (obj) {
    var newObj = {};
    for (var attr in obj) {
        newobj[attr] = obj[attr];
    }
    return newObj;
}
var obj2 = copy (obj);
obj2.a =;
alert (OBJ.A);   ==> 10

Second, the problem of the shallow copy of JavaScript

var obj = {
    a:{
        b:10
    }
}
function copy (obj) {
    var newObj = {};
    for (var attr in obj) {
        newobj[attr] = obj[attr];
    }
    return newObj;
}
var obj2 = copy (obj);
OBJ2.A.B =;
alert (OBJ.A.B); ==>20

deep copy of JavaScript

First, deep copy need to consider the concept of recursion
Deep copy not only copies the properties of the original object individually, but also recursively copies the objects contained in the original object to the new object, in turn, by using the deep copy method. This does not present the problem that the Arr attribute of obj and shallowobj above points to the same object.

recursive function (factorial) function
Digui (n) {
    if (n==1) {return
        1;
    }
    Return N*digui (n-1);
}

Alert (Digui (5));

Second, deep copy

var obj = {
    a:{
        b:10
    }
}
function deepcopy (obj) {
    if (typeof obj!= ' object ') {
        return obj;
    }
    var newObj = {};
    for (var attr in obj) {
        newobj[attr] = deepcopy (obj[attr));
    return newObj;
}
var obj2 = deepcopy (obj);
OBJ2.A.B =;
alert (OBJ.A.B); ==>10

deep copy and shallow copy of jquery

Deep and shallow copies of $.extend in JQ
var a = {};
var b = {name: {age:30}};

Shallow copy
$.extend (A, b);

Join parameter true is deep copy
$.extend (True, A, b);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.