I know that jQuery isn't designed for working with a class-like model but I really could do with being able to extend a base class as that fits my needs perfectly.
I started by just doing the following:
jQuery.myBase = {
foo: 'bar',
bar: function() { ... }
}
jQuery.fn.myPlugin = function() {
$.extend( this, jQuery.myBase, {
oof: 'rab',
rab: function() { ... }
}
}
That all works fine, I can access the base methods & properties via this
. That is until I try adding something like a jQuery event handler (etc.) which applies the event target to the this
.
So with the following:
jQuery.myBase = {
bar: function() { ... }
}
jQuery.fn.myPlugin = function() {
jQuery.extend( this, jQuery.myBase, {
init: function() {
jQuery('#someEl').click( this.onClick );
},
onClick: function(e) {
// this now references the element I bound the event to (<div id="someEl" />)
// so the following doesn't work
this.bar();
}
}
}
I've found a couple of things for class creation and inheritance that work with jQuery (such as John Resig's one and DUI) but those will/do experience the same issue.
So after all of that, how do I get to the original this
in these situations?
Update: The event handler (etc.) could be in either the jQuery.myBase
or the plugin itself.