JavaScript Clone Object Function

Mar
20

Here’s a pretty useful function that clones objects with the use of recursion allowing deep cloning:

function cloneObj(o) {
     if(typeof(o) != ‘object’) return o;
     if(o == null) return o;
   
     var newO = new Object();
   
     for(var i in o) newO[i] = cloneObj(o[i]);
      return newO;
 }

 

Original source snipplr.com.

One Response to “JavaScript Clone Object Function”

  1. very nice sites thanks admin…..

Leave a Reply