I’m having difficulty with calling a public method from within my plugin itself. I’m not sure what the issue is. What am I doing wrong?
(function($) {
var MyPlugin = function(){
/* PUBLIC */
this.publicFunction = function() {
// Do public things
};
/* PRIVATE */
var privateFunction = function() {
// Do private things
// Call this one public function
publicFunction(); // WANT THIS but doesn't work
}
};
$.fn.myPlugin = function() {
var myPlugin = new MyPlugin(options);
// Binding click to public function
$("a").bind('click', function(e){
e.preventDefault();
myPlugin.publicFunction($(this)); // AND WANT THIS but does
});
return myPlugin;
};
})(jQuery);
You just need to be able to reference the
thisobject of MyPlugin from within the function itself without losing scope. To do so assign it to a variable (name doesn’t matter, typical convention isself)