“官方”文档http://docs.jquery.com/Plugins/Authoring#Namespacing指出,应该按照以下方式向jQuery插件添加方法。我很少看到这种设计模式的实现。如果其他插件使用var方法,可能会发生冲突。这真的是首选方法吗?还是我应该采用不同的方法?
(function( $ ){
var methods = {
method1: function( options ) {},
method2: function( ) {}
};
$.fn.tooltip = function( method ) {
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
}
};
})( jQuery );