我要關閉我的窗口(只是一個絕對位置可拖動DIV)當我點擊的緊密聯繫OOP與回調
於JavaScript這是我的代碼
function ApplicationWindow() {
this.window = $('<div class="window"></div>');
this.create = function create() {
//.....
var closeButton = this.window.find('.close');
closeButton.live('click', this.close);
}
this.close = function close() {
this.window.fadeOut(200);
};
}
當我點擊在關閉按鈕上執行關閉功能,但問題是我得到一個錯誤「this.window is undefined」。這是因爲關閉函數是作爲回調傳遞的,但我的問題是我如何能夠很好地解決這個問題?
使用全局環境將函數綁定到變量不一定是一個乾淨的解決方案,但它會起作用。 – 2009-11-05 16:18:21
@Yuval:那不是全局的,它只是在ApplicationWindow函數的範圍內... – Seb 2009-11-05 16:22:00
@Yuval:如果你用'var'聲明變量,那麼你只將範圍設置爲當前函數。如果你不這樣做,那麼是的,全局範圍被用來代替。 – Seb 2009-11-05 16:22:54