2012-01-06 82 views
0

我有一個註冊爲鼠標移動事件的Javascript對象。但我的問題是事件參數沒有在我的自定義函數中傳遞,它始終未定義。註冊MouseMove:事件對象始終未定義

如果你看下面的函數touchMove(),你會發現參數事件總是未定義的出於某種原因?

我在做什麼錯?

MyObject.prototype.registerPointerEvents = function() 
{ 
    var instance = this; // function() { instance.func(); } 
    var ele  = this.getAttrib("divEle"); 

    ele.addEventListener("mousemove", function() { instance.touchMove(); }, false); 
} 

MyObject.prototype.touchMove = function(/*Event*/ event) 
{ 
    // Virtual function to be overridden in child classes 

    console.log("MOVE 1: "+event); // here it outputs event is undefined 
    if (!event) 
    event = window.event; 

    console.log("MOVE 2: "+event); // here event is still undefined 
    if (this.getAttrib("isDragging") == "true") 
    { 
    console.log("MOVE TRUE"); 
    this.getAttrib("divEle").style.left = event.clientX+"px"; 
    this.getAttrib("divEle").style.top = event.clientY+"px"; 
    } 
} 

回答

3

當然是未定義的,您正在調用一個不帶參數的匿名函數。

更好地利用這一點:

ele.addEventListener("mousemove", instance.touchMove, false); 

這是(幾乎)一樣

ele.addEventListener("mousemove", function(e) { instance.touchMove(e); }, false);