Javascript 最佳jQuery插件结构?

Javascript 最佳jQuery插件结构?,javascript,jquery,jquery-plugins,prototype,Javascript,Jquery,Jquery Plugins,Prototype,我正在尝试编写一个好的jQuery插件结构。我正试图遵循jQuery.com和其他网站的“最佳实践” 但我对原型有点困惑 我该不该用?实际结构看起来是好还是坏 谢谢 (function( $ ){ var defaults = { /* ... */ }, publicMethods = { add: function(options){ var $this = $(this); // ...

我正在尝试编写一个好的jQuery插件结构。我正试图遵循jQuery.com和其他网站的“最佳实践”

但我对原型有点困惑

我该不该用?实际结构看起来是好还是坏

谢谢

(function( $ ){
    var defaults = { /* ... */ },
    publicMethods = {
        add: function(options){
            var $this = $(this);
            // ...
            return $this;
        }
    },
    privateMethods = {
        init: function(options) {
            var $this = $(this);
            return $this;
        },
        click: function() {
            //...
        }
    };
    $.fn.tooltip = function(method) {
        var args = arguments;

        $(this).each(function() {
            if ( publicMethods[method] ) {
                return publicMethods[ method ].apply( this, Array.prototype.slice.call( args, 1 ));
            } else if ( typeof method === 'object' || ! method ) {
                return privateMethods.init.apply( this, args );
            } else {
                $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
            }   
        });
    };
})( jQuery );

关于使用
工具提示
函数的
.prototype
,只有当工具提示函数将作为构造函数调用时,这才有用

通常作为jQuery插件,您只对使用从
jQuery
构造函数创建的对象感兴趣,而不是从插件函数创建您自己的对象


在插件代码内部的某个地方可能会用到构造函数,但它通常不是实际的插件函数本身。

“我对原型有点困惑。”你困惑什么?在您使用它的唯一地方,您只能将它用作访问
.slice()
方法的一种手段。如果您遵循“最佳实践”,那么您是否有理由认为您的代码会很糟糕?哦,不,我只是在谈论代码的结构!我最近发现了“prototype”,所以我的问题是我应该继续使用我的“方法调用逻辑$.fn.tooltip…”还是开始使用prototype?如果这对您有帮助,jQuery只是将
fn
引用到
prototype
对象,大概是因为它比较短。因此,当你做
$.fn
时,你实际上是在做
$.prototype
。试试这个:
alert($.fn===$.prototype)@amnotiam谢谢,很高兴知道。但我真正的问题是我是否应该在插件中使用原型,比如tooltip.protype?