使用dojo在javascript中使用事件处理程序的实例变量。这是什么”?

时间:2013-01-16 14:43:42

标签: javascript javascript-events dojo instance

我无法在对象中创建“实例”变量,或者在将对象的函数作为事件处理程序调用时引用对象中的“实例”变量。下面是我正在使用的代码。如果重要的话,它(显然)使用dojo。我遇到的问题是否与我的案件有关?道场特有的?或者我是以完全不正当的方式解决问题?

dojo.declare("DynDraw", esri.toolbars.Draw, {
  constructor: function () {
    this.myLocalMap = arguments[0];
  }
  , myLocalMap: null
  , subscribed: false
  , activate: function (geometryType, options) {
    this.inherited(arguments);
    dojo.connect(this.myLocalMap, "onMouseDown", this.DynDraw_Map_OnMouseDown);
  }
  , DynDraw_Map_OnMouseDown: function (event) {
    //when called as an event handler (assigned above) “this.subscribed” is undefined
    //”this” seems to be “myLocalMap”, not the “DynDraw” object
    //when called as a “function” “this.subscribed” has a 
    if (this.subscribed == false) {
      dojo.connect(this.myLocalMap, "onMouseMove", this.DynDraw_Map_OnMouseMove);
      this.subscribed = true;
    }
  }
  , DynDraw_Map_OnMouseMove: function (event) {
      console.log("DynDraw_Map_OnMouseMove");
  }
});

1 个答案:

答案 0 :(得分:0)

您需要使用dojo.hitch。这将使范围与函数挂钩。

dojo.connect(this.myLocalMap, "onMouseDown", 
    dojo.hitch(this, this.DynDraw_Map_OnMouseDown));

http://dojotoolkit.org/reference-guide/1.7/dojo/hitch.html