好了一天後,我設法將問題縮小爲2行代碼。也許我試圖錯誤地使用這個語句。抓我的頭在JavaScript中的「這個」聲明。任何人請幫助
function scheduleItemView(myId){
this.update = function(show){
document.getElementById(this.id+'-title').innerHTML = show.title +": "+ show.startDate;
document.getElementById(this.id+'-title-overlay').innerHTML = show.title +": "+ show.startDate;
document.getElementById(this.id+'-description').innerHTML = truncate(show.description,190);
document.getElementById(this.id+'-time-start').innerHTML = show.startTime;
document.getElementById(this.id+'-time-end').innerHTML = show.endTime;
};
this.id=myId;
return true;
}
function nowNextView(){
this.now = new scheduleItemView('now');
this.next = new scheduleItemView('next');
this.update = function(type,args){
var myshow=args[0];
// problem is below. I have to use the global name to access the update method.
myNowNextView.now.update(myshow.now);
myNowNextView.next.update(myshow.next);
// whereas what I want to do is reference them using the "this" command like below.
// this.now.update(myshow.now);
// this.next.update(myshow.next);
// the above doesnt work. The update method in scheduleItemView is not seen unless referenced globally
// BUT even more infuriating, this.now.id does return "now" so it can access the object, just not the method
// any ideas?
};
}
對象然後用
var myNowNextView = new nowNextView();
實例化,然後我運行方法:
myNowNextView.update(stuff);
我試圖程序的主體內描述該問題。在代碼中沒有出現任何錯誤,我不得不做一個嘗試/捕獲,然後勉強地告訴我它找不到方法。 設計有瑕疵嗎?我可以不這樣做嗎? 非常感謝, 史蒂夫
其實它看起來像它應該工作。如果你調用'myNowNextView.update()',那麼'update'中的'this'將引用'myNowNextView'。 – 2011-03-09 21:29:04