function deepCopy(obj) { var type = Object.prototype.toString.call(obj); if(typeof obj !== "object" || obj === null) //boolean, number, string, undefined, function, null return obj; //object(Date, Boolean, Number, String, RegExp, Array, Object) if(type === "[object Date]") //object Date return new Date(obj.valueOf()); if(type === "[object Boolean]") //object Boolean return new Boolean(obj.valueOf()); if(type === "[object Number]") //object Number return new Number(obj.valueOf()); if(type === "[object String]") //object String return new String(obj.valueOf()); if(type === "[object RegExp]") //object RegExp return new RegExp(obj.valueOf()); if(type === "[object Object]" || type === "[object Array]") { var newObj = (type === "[object Object]") ? Object.create(Object.getPrototypeOf(obj)) : []; var props = Object.getOwnPropertyNames(obj); for(var item in props) { var des = Object.getOwnPropertyDescriptor(obj, props[item]); if(des.hasOwnProperty("value")) des.value = arguments.callee(des.value); Object.defineProperty(newObj, props[item], des); } return newObj; } return obj; }
function clone(Obj) { var buf; if (Obj instanceof Array) { buf = []; // 创建一个空的数组 var i = Obj.length; while (i--) { buf[i] = clone(Obj[i]); } return buf; } else if (Obj instanceof Object){ buf = {}; // 创建一个空对象 for (var k in Obj) { // 为这个对象添加新的属性 buf[k] = clone(Obj[k]); } return buf; }else{ return Obj; } }