JavaScript recursive simple implementation of object deep copy

Source: Internet
Author: User

Tags: Content compare objects Share Picture copy console object push Tor

The deep copy of the object in JavaScript has always been a bit more disgusting after all, there is no API directly full copy of their own convenience write recently in the project need to deep copy oneself simple sealed a method

Not much to say directly on the code

1<! DOCTYPE html>2<html lang= "en" >3<head>4<meta charset= "UTF-8" >5<meta name= "viewport" content= "Width=device-width, initial-scale=1.0" >6<meta http-equiv= "x-ua-compatible" content= "Ie=edge" >7<title>Document</title>8</head>9<body>Ten<script> One    functiondeepcopy (arr_obj) { A        //The constructor property returns a reference to the array function that created this object.  -Let Obj_arr = Arr_obj.constructor = = = = Array? [] : {}; -          for(Let keyincharr_obj) { the             //Object.prototype.toString.call () type judgment -             //Object.prototype.toString.call (Obj_arr) Returns [Object Object] or [object Array] -             if(Object.prototype.toString.call (Arr_obj[key]) = = = ' [Object Object] '){ -Obj_arr[key] =deepcopy (Arr_obj[key]); +}Else{ -                  +                 if(Object.prototype.toString.call (Arr_obj[key]) = = = ' [Object Array] '){ A                     //Console.log (Arr_obj[key]); atArr_obj[key].foreach ((Item,index) ={ -                         if(Object.prototype.toString.call (item) = = = ' [Object Object] '){ -Obj_arr[key][index] =deepcopy (item); -}Else{ -Obj_arr[key] = []; -Obj_arr[key][index] =item; in                         } -                     }) to}Else{ +Obj_arr[key] =Arr_obj[key]; -                 } the             } *         } $        Panax Notoginseng         returnObj_arr -    } theLet Obja = { +A:123, AB:[' A ', ' C ', {' A ': [' A ', ' B ', [' C ', ' d ', {e: ' C '}]]}], theC: ' yyyy ' +    } -Let OBJB =deepcopy (obja); $OBJA.A = 456 $Obja.b.push (' yyyy '); - Console.log (OBJA,OBJB); -     the</script> -</body>Wuyi</html>

The object that this method can copy so far has an array of object arrays with arrays and some nested things that have not been measured in complex data structures, but this is basically enough.

I changed obja.a and Obja.b.push (' yyyy ');

The comparison with the deep copy completed OBJB is as follows:

Copy succeeded

JavaScript recursive simple implementation of object deep 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.

Tags Index: