// What worksvaroriginal=newDate();varcopy=newDate(original);// What doesn't workcopy=original;// It should work, but it passes by reference so changes to copy also affect originalcopy=jQuery.extend(true,{},original);// Passes a useless object
functionclone(obj){varcopy;// Handle the 3 simple types, and null or undefinedif(null==obj||"object"!=typeofobj)returnobj;// Handle Dateif(objinstanceofDate){copy=newDate();copy.setTime(obj.getTime());returncopy;}// Handle Arrayif(objinstanceofArray){copy=[];for(vari=0,len=obj.length;i<len;i++){copy[i]=clone(obj[i]);}returncopy;}// Handle Objectif(objinstanceofObject){copy={};for(varattrinobj){if(obj.hasOwnProperty(attr))copy[attr]=clone(obj[attr]);}returncopy;}thrownewError("Unable to copy obj! Its type isn't supported.");}