Object reference (deep, shallow copy)

Source: Internet
Author: User

This existence referential relationship var a=[1,2,3];            var b=a;            B.push (4); alert (b);//1,2,3,4 alert (a);//1,2,3,4//This does not have a referential relationship//b re-assigned value will reopen an address var a=[1,2            , 3];            var b=a;            b=[1,2,3,4];            alert (b);//1,2,3,4 alert (a);//1,2,3 var obj1={a:10};            var obj2=obj1;            obj2.a=20;                        alert (obj1.a);//20;            var obj1={a:10};                function copy (obj) {//Shallow copy var newojb={};                for (var attr in obj) {newojb[attr]=obj[attr];            } return NEWOJB; }
If the above is Var obj1={a:{b:10}} It is necessary to perform deep lookup function deepcopy (obj) {//Deep copy var newojb={}; if (typeof obj1! = "Object") {//Termination condition return obj1;//returns the original 10} for (var attr in obj) {newojb[attr]=deepcopy (obj1[attr]);//recursive} return NEWOJB; } var obj2=copy (obj); obj2.a=20; Alert (obj1.a)//10

Object reference (deep, shallow copy)

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.