我想克隆原始对象和函数而不用引用,我的代码是否考虑克隆对象和函数的正确方法?具有功能的克隆对象
var apple = new function() {
this.type = "macintosh";
this.color = "red";
}
function aaa() {
return this.color + ' ' + this.type + ' apple';
};
var a = JSON.parse(JSON.stringify(apple))
var b =
JSON.parse(JSON.stringify(apple));
console.log(a)
a.getInfo = aaa
b.getInfo = aaa
a.color='green' // only a is green color
console.log(a.getInfo())
console.log(b.getInfo())
在的“克隆”你的对象甚至没有包含的功能 – Bergi
是的,我所示的例子功能是不是在对象但每一个问题我的时间发现似乎没有提到如何克隆对象中存在的函数,并通过JSON解析抛弃。 –
那么你现在还是不在乎功能呢?然后适当调整你的例子。 – Bergi