I have gone through various topics posted in stackoverflow and other forums, which explain how to write and extend a jquery plugin.
I wrote the following sample code to test my understanding
(function( $ ) {
$.fn.myPlugin = function() {
this.firstAPI(param)
{
alert(param);
};
return this;
};
})( jQuery );
Now I am extending the plugin.
function($) {
var extensionMethods = {
secondAPI: function(param){
alert(param);
}
};
$.extend(true, $[fn][myPlugin].prototype, extensionMethods);
})(jQuery);
Now, I am accessing the plugin $().myPlugin().firstAPI("Hello")
, which is successfully calling the API and showing the alert message. But I cannot access the secondAPI
by $().myPlugin().secondAPI("Hello")
. $().myPlugin()
object doesn't have the secondAPI
.
Also, one thing I have observed, myPlugin
object is initialized every time whenever I invoke a API on $().myPlugin()
object. I have also tried to pass a DOM element $('#divid')
and then call. It failed in this scenario also.