You can write a binding function and associate a context with an event handler.
Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift(); return function(){ fn.apply(object, args.concat(Array.prototype.slice.call(arguments))); } } function myclass() { this.count ; this.clicked = function(){ this.count++; }; this.init = function(){ $("div.mybtn").click(this.clicked.bind(this)); } this.getCount = function(){ alert(this.count); } }
source share