javascript - this in event handlers for another object -


class (mine) implements event handlers class b (3rd party). within these event handlers, access class a's properties.

using this in class a's handlers not work because references class b's scope.

global variables seem option. missing better alternative?

another solution bind event handlers object!

you first need add bind method function object. use code:

function.prototype.bind = function(scope) {   var func = this;    return function() {     return func.apply(scope, arguments);   } } 

now can register class b event handlers class a methods way:

var = new a(); var b = new b();  b.registerevent(a.eventhandlermethod.bind(a)); 

this way references this within code of a.eventhandlermethod point object a.

if need deeper understanding of stuff can read great article: http://www.alistapart.com/articles/getoutbindingsituations/

another article: http://alternateidea.com/blog/articles/2007/7/18/javascript-scope-and-binding


Comments

Popular posts from this blog

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c# - Making TableLayoutPanel Act Like An HTML Table (Cells That Resize Automatically Around Text) -