A common method to clone objects and arrays in JS _javascript tips

Source: Internet
Author: User
Ext's two methods of cloning:

You can clone objects, data, and so on: var Newjson = Ext.clone (JSON);
Only arrays can be cloned: var Newjson = Ext.Array.clone (JSON);

The jquery approach:

Deep copy "can iterate": var Newjson = Jquery.extend (true,{}, JSON);
Shallow copy "Cannot iterate": var Newjson = Jquery.extend ({}, JSON);

var Newjson = $.map (Json,function (n) {return n;});

Array Cloning method:
Copy Code code as follows:

var Newjson = Json.concat ();
var Newjson = json.splice (0);


These methods are to copy a new object or array, but if the parent "IFRAME situation" in the way to the parents of the object of the problem "there is a reference relationship", you need to pay attention to the way to transfer data.

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.