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.
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;