jquery插件$.extend

jquery插件$.extend,jquery,plugins,namespaces,extend,Jquery,Plugins,Namespaces,Extend,如果我扩展jqueryfn(比如$.fn.extend),我会编写我的插件: (function($){ $.fn.extend({ functionName : function(){ //function returns return this.each(function(){ $(this).append("<div>text</div>");

如果我扩展jquery
fn
(比如$.fn.extend),我会编写我的插件:

(function($){
    $.fn.extend({
        functionName : function(){
             //function returns
             return this.each(function(){
                  $(this).append("<div>text</div>");
             });
        }
    });
})(jQuery);

我不知道的是如何写“return”在这种情况下

你可以在第二个实例中返回你喜欢的任何东西。例如,想想
$.each()
$.get()
。然而,如果我是你,我会避免使用这个函数名称空间-它可能会导致污染。相反,您应该保留此名称空间,以便在jquery名称空间下添加您自己的名称空间,如:

(function($){
    $.extend({
        myNamspace : {
          functionName: function(){
            // return whatever
          }
        }
    }});
})(jQuery);

更新

当您执行涉及多个选择器的多个操作时,您必须决定什么最有意义。如果一个选择器是主要焦点,但也会影响其他项目,请像插件一样编写它,并返回如下主要结果集:

(function($){
    $.extend({
        functionName : function(){
             //function returns

        }
    });
})(jQuery);
$.fn.myAction = function(secondarySelector){
      return this.each(function(){
          $(this).css('display', 'block');
          $(secondarySelector).hide();
      });
};

// Use:
// $(primarySelector).myAction(secondarySelector);
$("#id").myAction(".address"); // Chain continues after action
(function($){

    // The if statement allows the file to be used with
    // other files that use the same shared namespace
    if(!$.Namespace){
        $.Namespace = { };
    };

    $.Namespace.Constructor = function( params ){
       ... code ...
    };

    // And for the wrapper sets ($.fn):
    $.fn.functionName = function(opts){
       return this.each(function(){
          ... code ...
       });
    };

})(jQuery);
如果选择器的重要性都相同,则只需创建一个函数,该函数不返回任何内容或根据成功情况返回
true
/
false

构建代码的另一种方法:

extend方法用于其他OOP框架,如您所示,也可以与jQuery一起使用。但是,您会发现,许多jQuery开发人员选择了更短更明显的语法,如:

(function($){
    $.extend({
        functionName : function(){
             //function returns

        }
    });
})(jQuery);
$.fn.myAction = function(secondarySelector){
      return this.each(function(){
          $(this).css('display', 'block');
          $(secondarySelector).hide();
      });
};

// Use:
// $(primarySelector).myAction(secondarySelector);
$("#id").myAction(".address"); // Chain continues after action
(function($){

    // The if statement allows the file to be used with
    // other files that use the same shared namespace
    if(!$.Namespace){
        $.Namespace = { };
    };

    $.Namespace.Constructor = function( params ){
       ... code ...
    };

    // And for the wrapper sets ($.fn):
    $.fn.functionName = function(opts){
       return this.each(function(){
          ... code ...
       });
    };

})(jQuery);

好吧,但我对“回报任何东西”感兴趣;我该怎么写呢?如果我只想返回一行,比如“return$(selector).append(…)”,它的工作原理是这样的……但是如果我想返回多个“actions”,比如“$(selectorOne).appned()”,我应该如何编写它呢$(两个)。删除();'然后返回数组、对象或jquery节点列表。但是返回的内容取决于您希望函数返回的内容。因此,对于我要执行的操作,我不再需要“return”关键字了?(如return this.each())@kmunky我添加了一个关于如何处理的答案。这对你的特殊需要有意义吗?pff..很难说。我认为我的问题不是这个。我会提出一个新问题,谢谢你这篇好文章