Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不要再听魔咒了_Javascript_Prototype_Webos - Fatal编程技术网

Javascript 不要再听魔咒了

Javascript 不要再听魔咒了,javascript,prototype,webos,Javascript,Prototype,Webos,我在一个webOS应用程序中有几个事件侦听器,每个侦听器的设置如下: this.controller.listen( 'aWidget', Mojo.Event.widgetEvent, this.respondToWidgetEvent.bindAsEventListener(this) ); this.controller.stopListening( 'aWidget', Mojo.Event.widgetEvent, this.r

我在一个webOS应用程序中有几个事件侦听器,每个侦听器的设置如下:

this.controller.listen(
    'aWidget', 
    Mojo.Event.widgetEvent, 
    this.respondToWidgetEvent.bindAsEventListener(this)
);
this.controller.stopListening(
    'aWidget', 
    Mojo.Event.widgetEvent, 
    this.respondToWidgetEvent.bindAsEventListener(this)
);
为了停止监听,我编写了如下代码:

this.controller.listen(
    'aWidget', 
    Mojo.Event.widgetEvent, 
    this.respondToWidgetEvent.bindAsEventListener(this)
);
this.controller.stopListening(
    'aWidget', 
    Mojo.Event.widgetEvent, 
    this.respondToWidgetEvent.bindAsEventListener(this)
);

然而,我现在意识到,我的听众可能不会停止。当我对一个函数调用
bindAsEventListener
时,是否每次都返回相同的对象?如果没有,是否停止侦听确保删除相应的侦听器?

如果我记得正确,则每次调用bindAsEventListener()都会返回一个新实例。通过调用一次并设置变量来防止该操作:

bindToWidget = this.respondToWidgetEvent.bindAsEventListener(this);

this.controller.listen(
    'aWidget', 
    Mojo.Event.widgetEvent, 
    bindToWidget
);

this.controller.stopListening(
    'aWidget', 
    Mojo.Event.widgetEvent, 
    bindToWidget
);

谢谢我怀疑是这样的,因为我以前在一些人的代码中看到过这种模式,但我也看到过它是以我的方式完成的。现在我确认了另一个带有额外变量的模式是正确的。