good day

Already a lot of work with jQuery. But now there is a periodic question about the creation of plug-ins. Namely, the question about the events.

If, let's say, we have an example:

test= function(options) { this.init(options); }; $.extend(test.prototype, { init: function() { } }); 

Question: if in the init method and select a DOM element and assign it a click event. Those. Is there an acceptable way for the method of the test object to be the event handler and for the initialized object to be preserved? That through this methods test and all its properties were accessible to me.

Thank.

    1 answer 1

    For this purpose there is jQuery.proxy :

     var test = function(options) { this.init(options); } $.extend(test.prototype, { field: 'hello world', init: function(options) { $(options).click($.proxy(this.handler, this)); }, handler: function() { alert(this.field); } }); $(function() { var t = new test('#myButton'); }); 

    Documentation: jQuery.proxy