Class A (mine) implements event handlers for class B (3rd party). Within these event handlers, I would like to access class A’s properties.
Using this in class A’s handlers does not work because it references class B’s scope.
Global variables seem like the only option. Am I missing a better alternative?
Another solution is to bind your event handlers to your object!
You first need the add the
bindmethod to any function object. Use this code:Now you can register your
class Bevent handlers to yourclass Amethods this way:This way any references to
thiswithin the code ofA.eventHandlerMethodwill point to objecta.If you need a deeper understanding of this stuff you can read this great article: http://www.alistapart.com/articles/getoutbindingsituations/
Another article: http://alternateidea.com/blog/articles/2007/7/18/javascript-scope-and-binding