Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;“私人”;jQuery插件_Jquery_Jquery Plugins - Fatal编程技术网

&引用;“私人”;jQuery插件

&引用;“私人”;jQuery插件,jquery,jquery-plugins,Jquery,Jquery Plugins,我在互联网上搜寻答案,但毫无结果。我觉得答案可能很简单,但我一直在逃避 我正在编写jQuery插件,并遵循所有最佳实践。假设我的插件名为foo,我有以下标准插件定义: (function($){ $.fn.foo = function() { this.append('<p>I'm doing stuff!</p>'); } })(jQuery); (函数($){ $.fn.foo=函数(){ 这个.append(“我在做事!”); } })(jQuer

我在互联网上搜寻答案,但毫无结果。我觉得答案可能很简单,但我一直在逃避

我正在编写jQuery插件,并遵循所有最佳实践。假设我的插件名为foo,我有以下标准插件定义:

(function($){
  $.fn.foo = function() {
    this.append('<p>I'm doing stuff!</p>');
  }
})(jQuery);
(函数($){
$.fn.foo=函数(){
这个.append(“我在做事!

”); } })(jQuery);
到目前为止相当基本。现在让我们假设我有一些内部功能,我想封装在一个名为bar的函数中。不过,这里有一个陷阱:我想把它称为jQuery插件,这样我就可以利用链接等。也就是说,我希望能够像这样使用它:

(function($){
  /* this is bad: it clutters up the namespace, and exposes implementation details:
  $.fn.bar = function(text) {
    this.append('<p>'+text'</p>');
  }
  */
  $.fn.foo = function() {
    this.append('<p>I'm doing stuff!</p>').bar('bar does something else!');
  }
})(jQuery);
(函数($){
/*这是不好的:它会弄乱名称空间,并公开实现细节:
$.fn.bar=函数(文本){
追加(“”+文本“

”); } */ $.fn.foo=函数(){ this.append(“我正在做事情!

”).bar('bar做其他事情!'); } })(jQuery);
我如何声明bar以便像jQuery插件一样调用它,但它在我的插件范围之外不可用

我在使用Javascript的apply方法时弄糟了,我得到了一些有用的东西,但它很笨重,不比用jQuery对象作为参数调用函数好多少


我相信有一个简单的解决方案……有人吗?

Ethan,使用提倡的插件模式,存在一系列的可能性,包括私有函数和一系列公共方法,所有这些都在一个插件中

您可以拥有私有函数,并且可以想象它们是可链接的(某种),但是:

  • 您通常无法在内部进行链接,因为内部调用通常使用
    .call()
    进行
  • 您通常不希望或不需要内部的可链接性,因为公共方法的形式通常是
    返回this.each(function(){…}),在这个循环中,代码处理它所处理的jQuery选择的单个元素
    
例如:

(function($){
    // **********************************
    // ***** Start: Private Members *****
    var pluginName = 'foo';
    var cough = function(text, bgColor) {
        text = text || ''; 
        bgColor = bgColor || '#FFF';
        $(this).append($('<p/>').append(text).css('backgroundColor', bgColor));
    };
    // ***** Fin: Private Members *****
    // ********************************

    // *********************************
    // ***** Start: Public Methods *****
    var methods = {
        init: function(text) {
            text = text || 'foo init!';
            return this.each(function() {
                methods.bar.call($(this), 'cough from bar from init');
                cough.call($(this), 'cough from init');
            });
        },
        bar: function(text) {
            text = text || 'cough from bar!';
            return this.each(function() {
                cough.call(this, text, '#99CC99');
            });
        }
    };
    // ***** Fin: Public Methods *****
    // *******************************

    // *****************************
    // ***** Start: Supervisor *****
    $.fn[pluginName] = 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 );
        } else {
            $.error( 'Method ' + method + ' does not exist in jQuery.' + pluginName );
        }
    };
    // ***** Fin: Supervisor *****
    // ***************************
})(jQuery);
但是,
cough
无法从外部调用


注意:在上面的模式中,主管总是完全相同的-它不需要编辑。

您可以使用闭包来包装您的私有方法。i、 e在模块化设计上制作插件。
$("div").foo(); //same as $("div").foo(init');
$("div").foo('bar', 'cough from bar');