There are several ways to do this, but the one I prefer is as follows:
$.fn.extend({ pluginName: function( options ) { this.defaultOptions = {}; var settings = $.extend({}, this.defaultOptions, options); return this.each(function() { var $this = $(this);
The disadvantage of this is pluginMethods, accessible to everyone. But you can solve this by moving it in the same closure as the plugin declaration:
(function($){ $.fn.extend({ pluginName: function( options ) { this.defaultOptions = {}; var settings = $.extend({}, this.defaultOptions, options); return this.each(function() { var $this = $(this); $this.data('pluginName', new pluginMethods($this)) }); } }); function pluginMethods($el){
source share