function cloneFun(obj){ if(!obj||"object" != typeof obj){ return null; } var result = (obj instanceof Array)?[]:{}; for(var i in obj){ result[i] = ("object" != typeof obj[i])?obj[i]:cloneFun(obj[i]); } return result; }
例:
var objTest = { param1:{ p1:1, p2:2, p3:3 }, param2:4, param3:5 }; var arrTest = [1,[2,3,4],5]; var objResult = cloneFun(objTest); var arrResult = cloneFun(arrTest);