function clone(o) { var temp = {} for (var key in o) { if (typeof o[key] == 'object') { temp[key] = clone(o[key]) } else { temp[key] = o[key] } } return temp }
测试:
var obj = {qwe:123,asd:456};
var obj1 = obj,obj2 = clone(obj);
obj1.qwe = 456;
obj2.asd = 'asdzxc';
console.log(obj);//{qwe: 456, asd: 456}
console.log(obj1);//{qwe: 456, asd: 456}
console.log(obj2);//{qwe: 123, asd: "asdzxc"}