
| home | AJAX (8) || C#.NET (7) || Coldfusion Development (16) || DHTML (15) || Flash Development (19) || jQuery (8) || MSSQL (2) || UNIX (10) |
| 7.11.07 | Javascript Recursive Object Copy, Deep Object Copy, Pass By Value |
Use jQuery’s jQuery.extend( TRUE, target, object1, [objectN] ) “DEEP EXTEND” instead (only works in version >= 1.2.6)
update 11/5/2007
So a “problem” in Javascript is that there is no direct access to passing by value for certain objects like there is in C. There’s no easy way to setup a pointer or even write a custom “=” operator overload function. Although the default pass by reference in Javascript for objects, arrays, functions, etc. (non “simple” data types) passing by value is needed. Hence this nifty function that basically handles a recursive copy of objects.
I must disclaim that it does not handle ‘function’ typeof()’s - but it works for simple objects within objects within objects, and arrays of objects of arrays of objects. You get the idea. This could easily be modified to copy functions but I left it out so that it wouldn’t copy over the prototyped json functions that I use all over the application via http://www.json.org/js.html.
Here’s the code:
Download this code: deepObjCopy.js
| Nikolaj on 7.11.07 at 2pm |
|
// Put this in your pipe and smoke it |
1 Comment