1
如何從另一個對象實例訪問此對象?從方法實例訪問此變量
var containerObj = {
Person: function(name){
this.name = name;
}
}
containerObj.Person.prototype.Bag = function(color){
this.color = color;
}
containerObj.Person.prototype.Bag.getOwnerName(){
return name; //I would like to access the name property of this instance of Person
}
var me = new Person("Asif");
var myBag = new me.Bag("black");
myBag.getOwnerName()// Want the method to return Asif
@MrCode:謝謝,當然它應該。如果你只是編輯帖子,我不會介意:-) – Bergi
+1沒問題,下次我會編輯:) – MrCode
我在調用makeBag方法後設置了me.name =「Smo」。然後myBag.getOwnerName()返回John。這就是我想要的,但爲什麼會這樣呢?我的意思是,當傳入的對象改變時,爲什麼this.owner會改變? –