這是My Old Question我可以保持一個對象的副本,該對象內部時,它的創建 - 續:
延續這是我的函數,它創建了一個新的學生對象:
function student(id, name, marks, mob, home){
this.id = id;
this.name = name;
this.marks = marks;
this.contacts = {};
this.contacts.mob = mob;
this.contacts.home = home;
this.toContactDetailsString = function(){
return this.name +':'+ this.mob +', '+ this.home
}
}
我想在對象內部初始化時創建一個對象的副本: 我想出了這個:
function student(id, name, marks, mob, home){
this.id = id;
this.name = name;
this.marks = marks;
this.contacts = {};
this.contacts.mob = mob;
this.contacts.home = home;
this.toContactDetailsString = function(){
return this.name +':'+ this.mob +', '+ this.home
}
this.baseCopy = this; //Not sure about this part
}
但問題是它給了我一個無限循環的拷貝當前對象在baseCopy中;並且,當我更新我的對象的任何屬性時,它也會自動更新。
1.如何在創建對象時保留具有初始值的對象的副本?
2.是否有可能不使用純JS
您不會將對象的副本分配給「baseCopy」,而只是引用。沒有複製完成,這就是爲什麼「baseCopy」總是「最新」。查看http://api.jquery.com/jQuery.extend/以瞭解如何獲得實際副本。 – Niko
'this'似乎指的是這個上下文中的'window'對象。 – Stefan
那麼,是否有任何答案張貼爲你工作? – Cerbrus