/ Published in: JavaScript
Clones and object (associative array - not arrays). Goes deep.
Required sometimes when you need to keep the original object intact, i.e. when you pass an object in to a function and modify it in the function - the original would change.
Expand |
Embed | Plain Text
function clone(o) { if(typeof(o) != 'object') return o; if(o == null) return o; var newO = new Object(); for(var i in o) newO[i] = clone(o[i]); return newO; }
Comments
Subscribe to comments
You need to login to post a comment.

Line 2,3 could more effectively be written as: if(typeof(o)!="object"||o==null)return o;
I am getting "Out of stack space" error while executing newO[i] = clone(o[i]);
I am trying to copy a event to new variable for future use in next event.
What can be the workaround? Please advice,
Thanks, Laxman.
I am getting "Out of stack space" error while executing newO[i] = clone(o[i]);
I am trying to copy a event to new variable for future use in next event.
What can be the workaround? Please advice,
Thanks, Laxman.
I am getting "Out of stack space" error while executing newO[i] = clone(o[i]);
I am trying to copy a event to new variable for future use in next event.
What can be the workaround? Please advice,
Thanks, Laxman.