Deep cloning of JavaScript objects

Source: Internet
Author: User

I don't know when it started, a new word emerged from the front circle: Object depth cloning. Looks like very tall on the appearance, actually is not new, in our actual project development, you may have already used, but because the Chinese character's profound, some originally very simple thing to be some seemingly specialized words slightly decorates, became mysterious up.

First, why do you want to make a deep clone of an object? Let me make a guess: you will sometimes think that JS's built-in object document is too long, and you might do this:

var d = document;d.by = function (ID) {    return d.getElementById (ID);}; D.by (' id '). innerHTML = ' Hello Sentsin ';

Code laycode-v1.1

The code above simplifies document.getElementById and also adds a by member method to the original document object, and you can verify your judgment by Document.hasownproperty the status value returned by the (' by '). Let's look at an example below.

var person = {name: ' Virtuous heart ', profession: ' Front-end development ', place: ' Hangzhou '};var newperson = Person;newperson.age = ' "; Console.log (person) ;//Result: {name: ' Virtuous heart ', profession: ' Front-end development ', place: ' Hangzhou ', age:24}
Code laycode-v1.1

This shows that when an object is simply passed to a new variable, it simply adds an alias to the object. This means that by manipulating the alias, the original object key value is changed. But the problem is that sometimes we want Newperson to be completely independent of the person, there is no synchronization relationship between them, then we need to generate a copy, see example:

var cloneobj = function (obj) {    var str, newobj = Obj.constructor = = = Array? [] : {};    if (typeof obj!== ' object ') {        return;    } else if (window. JSON) {        str = json.stringify (obj),//Serialization object        newobj = Json.parse (str);//Restore    } else {for        (var i in obj) {            Newobj[i] = typeof obj[i] = = = ' object '?             Cloneobj (Obj[i]): Obj[i];         }    }    Return newobj;};/ /test var obj = {a:0, b:1, c:2};var arr = [0, 1, 2];//perform deep clone var newobj = cloneobj (obj), var newarr = Cloneobj (arr);//For new after cloning object to delete delete Newobj.a;newarr.splice (0,1); Console.log (obj, arr, newobj, newarr);//Result: {a:0, b:1, c:2}, [0, 1, 2], {b: 1, C:2}, [1, 2];
Code laycode-v1.1

This is called object cloning. But there are a couple of places to explain. JSON objects and their member methods in code stringify and parse belong to the ECMASCRIPT5 specification, which are responsible for converting an object (including an array object) into a string, and restoring it, to implement a deep copy of the object. Then for the low-level browser (IE), copy the array, you can use Newobj.concat (obj), and ordinary objects, simply enumerate the value of the assignment.

Deep cloning of JavaScript objects

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.