【发布时间】:2012-10-24 12:56:12
【问题描述】:
“官方”文档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 );
【问题讨论】:
-
我想由于我没有收到任何其他答案或 cmets,prodigitalson 的答案完全同意......
标签: jquery