Document Information

Last modified:
2007/05/31 18:19 by thron7

Problems with "this" in event handlers

How do I ensure that the correct “this” is referred to in an event handler? Say you have an event-handler within a custom widget which looks like this:

MyWidget.prototype._someHandler = function(e) {
	alert(this);
}

and then later within the same class definition, register a handler with another class instance:

var anotherWidget = new AnotherWidget();
anotherWidget.addEventListener("changeSomething", this._someHandler);

When the handler gets triggered by a “changeSomething” event, the alert of the handler is being called. However, there is a problem in that ‘this’ now refers to an object of class AnotherWidget and not to the instance of MyWidget. To solve this problem, use:

anotherWidget.addEventListener("changeSomething", this._handler, this);

Information

Last modified:
2007/05/31 18:19 by thron7

Account

Not logged in

 
 

Job Offers

To further improve qooxdoo we are seeking javascript developers. Read more...

Rich Ajax Platform (RAP)

RAP uses qooxdoo, Java and the Eclipse development model to build rich web applications. Read more...

qooxdoo Web Toolkit (QWT)

Similar to GWT this framework allows to create impressive qooxdoo applications just using Java. Read more...

Pustefix

Pustefix is a MVC-based web application framework using Java and XML/XSLT. Read more...

 
SourceForge.net Logo

Bad Behavior has blocked 0 potential spam attempts in the last 7 days.