The easiest way to bind a call to handle_click to the object that he defined would be something like this:
var self=this; document.getElementById('x').onclick = function(e) { return self.handle_click(e) };
If you need to pass parameters or want the code to look cleaner (for example, if you configure many similar event handlers), you can use the currying technique to achieve the same:
bind : function(fn) { var self = this; // copy arguments into local array var args = Array.prototype.slice.call(arguments, 0); // returned function replaces first argument with event arg, // calls fn with composite arguments return function(e) { args[0] = e; return fn.apply(self, args); }; },
...
document.getElementById('x').onclick = this.bind(this.handle_click, "this parameter is passed to handle_click()", "as is this one");
Shog9
source share