2013-04-23 26 views
0

我正在開發骨幹應用程序。我正在使用調用select函數的主幹添加mousedown事件。 select函數裏面我設置了timeout,它調用另一個函數selection。在selection函數中我想用console.log(this.el)來控制currently clicked element。但是,this.el未定義,因爲這不涉及我當前的模塊。 如何保留這個關鍵字,以便我可以在選擇功能中使用它?如何在以下場景中保留此關鍵字

這裏是我的代碼

events: { 
     'mousedown': 'select', 
     'mouseup': 'deselect' 
    }, 


    select: function() { 
     this.timeoutId = setTimeout(this.selection, 1000); 
    }, 

    deselect: function() { 
     clearTimeout(this.timeoutId); 
    }, 

    selection: function() { 
     console.log(this.el); 
    } 

回答

2

你可以解決這個問題如T他:

select: function() { 
    var self = this; 
    this.timeoutId = setTimeout(function() { 
     self.selection(); 
    }, 1000); 
} 

許多瀏覽器還支持bind功能,結合對象作爲this的功能

select: function() { 
    this.timeoutId = setTimeout(this.selection.bind(this), 1000); 
} 
+0

@downvoter - 護理闡述? – NilsH 2013-04-23 07:22:10