addEventListener及其范围 [英] addEventListener and the scope of this

查看:113
本文介绍了addEventListener及其范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有第三方Flash对象,我可以通过他们提供的JavaScript API进行操作。我试着在这个对象上听一个事件,然后在我的对象中触发事件来进一步冒泡事件。我碰巧使用的是EXT Js,但我认为它不重要。

I have a third party flash object which i can manipulate through a javascript API they provided. I am tryind to listen to an event on this object and then fire event inside my object to further bubble up the event. I happen to being using EXT Js but i dont think its important here.

示例代码

this.chart.addEventListener('create', function() {
    this.fireEvent('created');
}, false)

我的问题是匿名函数中的'this'是指触发事件的对象,而不是我要触发事件的对象on。

My problem is that 'this' inside the anonymous function refers to the object that fired the event rather than my object that I want to fire an event on.

这是另一个范围问题。在此先感谢您的帮助。

Its yet another scope issue. Thanks in advance for any help.

推荐答案

这是解决此问题的典型方法: -

This is the typical approach to this problem:-

(function(self) {
  self.chart.addEventListener('create', function() {self.fireEvent('created');}, false);
})(this);

这篇关于addEventListener及其范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆