jquery Deep Clone JavaScript Object

Source: Internet
Author: User

Using jquery's extend approach:
extend ([param1,]obj1,obj2);
Description:1.> Copying an object obj2 to Obj1

2>PARAM1, which is a Boolean type, True indicates a deep copy
eg
  1. var  Object1 = {
  2. Apple : 0,
  3. Banana : {weight: *, price: +},
  4. Cherry :
  5. };
  6. var  Object2 = {
  7. Banana : {price: +},
  8. Durian :
  9. };
  10.     $. Extend (True,object1, object2); //Deep copy, to be copied with a new object, Object1 can be written as {}
  11. object2. Banana . Price =100; //Modify Values
  12. Alert (object1. Banana . Price )//Deep copy, the value in Object1 does not change

JavaScript implementations:

  1. function Cloneobject (obj) {
  2. var clone = {};
  3. for (var i in obj) {
  4. if (typeof(obj[i]) == "Object")
  5. clone[i] = cloneobject(obj[i]);
  6. Else
  7. Clone [i] = obj[i];
  8. }
  9. return clone;
  10. }
eg
    1. function Cloneobject (obj) {
    2. var clone = {};
    3. for (var i in obj) {
    4. if (typeof(obj[i]) == "Object")
    5. var Object1 = {
    6. Apple : 0,
    7. Banana : {weight: *, price: +},
    8. Cherry :
    9. };
    10. var obj= cloneobject (object1 );
    11. Object2 . Banana . Price =200; //Modify Values
    12. alert (obj. Banana . Price) //Output still

JQuery Deep-clone JavaScript objects

Related Article

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.