jquery:我怎样才能链接函数?

Chi*_* Zu 1 javascript jquery jquery-plugins

我使用jquery很长一段时间,并考虑自己创建一个插件.但无论教程多么简单,我都无法理解链接的想法.假设我有这个非常简单的插件....

(function($){
      $.test = function(){

        $.test.one = function(){ alert("1");};

        $.test.two = function(){ alert("2");};

        return this.each(function(){
             (what to write here????)
        });
      };
 })(jQuery);
Run Code Online (Sandbox Code Playgroud)

我想要完成的是,当我打电话时,例如

var myplugin = $.test();
myplugin.one().two();
Run Code Online (Sandbox Code Playgroud)

但它给我带来了错误.对不起,我多次尝试谷歌搜索简单的教程,但我不能让这个工作

fca*_*ran 7

你想这样做吗?

(function($){
    $.fn.test = function(){
        var self = this;
        this.one = function(){ alert("1"); return self; };
        this.two = function(){ alert("2"); return self; };
        return this;
    };
}(jQuery));


var myplugin = $().test();
myplugin.one().two();
Run Code Online (Sandbox Code Playgroud)

示例小提琴:http://jsfiddle.net/kzzMY/

作为旁注,我强烈建议这个主题的有用资源:http://jqueryboilerplate.com/