Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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 Superfish插件:$.fn.extend({…})——这段代码是如何工作的?_Jquery_Plugins_Overriding - Fatal编程技术网

Jquery Superfish插件:$.fn.extend({…})——这段代码是如何工作的?

Jquery Superfish插件:$.fn.extend({…})——这段代码是如何工作的?,jquery,plugins,overriding,Jquery,Plugins,Overriding,Superfish JS是 以下是相关代码: $.fn.extend({ hideSuperfishUl : function() { // ...code here... return this; }, showSuperfishUl : function(){ // ...code here... return this; } }); 问题: 是否扩展({…})允许用户覆盖hideSuperfi

Superfish JS是

以下是相关代码:

$.fn.extend({
    hideSuperfishUl : function() {
        // ...code here...
        return this;
    },
    showSuperfishUl : function(){
        // ...code here...
        return this;
    }
});
问题:

是否扩展({…})允许用户覆盖
hideSuperfishUl
showSuperfishUl
功能?如果是,调用插件时更改这些方法的语法是什么样的:

<script> 

    $(document).ready(function() { 
        $('ul.sf-menu').superfish(); // How to override hideSuperfishUl &/or showSuperfishUl?
    }); 

</script>

$(文档).ready(函数(){
$('ul.sf menu').superfish();//如何重写hideSuperfishUl和/或showSuperfishUl?
}); 

Superfish是前不久写的。。。这仍然是允许用户覆盖插件功能/方法/其他的最佳方式吗?如果没有,有“最好”的方法吗?

在这里尝试回答我自己的问题如果我在下面写的东西有错,请纠正我;)


他说:

延伸 在我看来,这是创建jQuery最不惯用的方法 插件。它使用
jQuery.fn.extend
方法而不是
jQuery.fn.pluginName

(function($){
  $.fn.extend({
      myPlugin: function() {
          return this.each(function(){
              // do something
          });
      },
      myOtherPlugin: function() {
          return this.each(function() {
              // do something
          });
      }
  });
})(jQuery);
如果需要添加大量数据,您会发现此结构很有用 将插件方法添加到jQuery。然而,我认为,如果 插件需要添加很多方法,可能有更好的方法 构建其实现

我觉得extend方法更多地被程序员使用 来自其他JS库。我个人觉得简单的
$.fn.pluginName
=
结构更易于阅读,并且更符合您通常在jQuery插件中看到的内容

SuperfishJS使用的代码与上面引用的代码相同;长话短说,
show superfishul()
hideSuperfishUl()
是jQuery
$.fn
名称空间中的附加插件

我能够覆盖上述功能,如下所示:

jQuery(function(){

    $.fn.showSuperfishUl = function() {
        console.log('OMG');
        return this;
    };

    $.fn.hideSuperfishUl = function() {
        console.log('BECKY');
        return this;
    };

    jQuery('ul.sf-menu').superfish();
});
接下来,他说:

在适用的情况下,提供对辅助功能的公共访问
//插件定义
$.fn.hilight=函数(选项){
//迭代并重新格式化每个匹配的元素
返回此值。每个(函数(){
var$this=$(this);
// ...
var markup=$this.html();
//调用我们的格式化函数
markup=$.fn.hilight.format(标记);
$this.html(标记);
});
};
//定义我们的格式函数
$.fn.hilight.format=函数(txt){
返回“”+txt+””;
};
我们可以很容易地支持选项上的另一个属性 对象,该对象允许提供回调函数来重写 默认格式。这是另一个很好的支持方式 自定义插件。这里展示的技术采用了 进一步,实际公开format函数,以便 重新定义。有了这项技术,其他人就有可能 发布自己的插件自定义覆盖,换句话说,它 意味着其他人可以为你的插件编写插件

结论:
$.fn.extend
$.fn.pluginName.function
都是类似的,允许最终用户轻松覆盖功能;前者向jQuery
$.fn
名称空间添加新插件,而后者是包含在单个
$.fn.pluginName
名称空间中的函数

说:

不要在jQuery.fn对象中为每个对象设置多个命名空间 插件


。。。因此,我选择的“最佳”技术是
$.fn.pluginName.function

你看过jQuery的文档了吗?@JaredFarrish谢谢你的回复!我确实读过文档,但我不完全理解它与Superfish的关系。不过现在我们正在重新阅读文档。谢谢
// plugin definition
$.fn.hilight = function(options) {
  // iterate and reformat each matched element
  return this.each(function() {
      var $this = $(this);
      // ...
      var markup = $this.html();
      // call our format function
      markup = $.fn.hilight.format(markup);
      $this.html(markup);
  });
};
// define our format function
$.fn.hilight.format = function(txt) {
  return '<strong>' + txt + '</strong>';
};