1.实现:
var ObjectUtil = (function() {
function clone(obj) {
var json = JSON.stringify(obj);
var newObj = JSON.parse(json);
return newObj;
}
return {
clone: clone,
}
})();
2.测试:
var obj = {
name: "leo",
age: 18
}
var obj2 = ObjectUtil.clone(obj);
obj.age = 30;
obj2.age = 20;
console.log(obj);//Object {name: "leo", age: 30}
console.log(obj2);//Object {name: "leo", age: 20}
借助JSON实现,简单粗暴实现浅克隆