【问题标题】:What does passing params in a jQuery plugin mean?在 jQuery 插件中传递参数是什么意思?
【发布时间】:2013-01-24 09:40:32
【问题描述】:

我见过很多插件,其中包括文档、窗口、未定义等对象到打开和关闭的参数中。

这有必要吗? 这是什么意思? 什么时候应该使用这些?

;(function( $ , document, window, undefined) {
        "use strict";        

      $.fn.pluginname= function(options) {      

        //Code

      };
    })( jQuery, document, window, undefined);

【问题讨论】:

  • 一般来说,它只是让缩小代码更有效。将 jQuery 传递给它允许插件使用$,而不管其他库是否已控制它。

标签: javascript jquery jquery-plugins jquery-events


【解决方案1】:

来自jqueryboilerplate.com

 // undefined is used here as the undefined global variable in ECMAScript 3 is
 // mutable (ie. it can be changed by someone else). undefined isn't really being
 // passed in so we can ensure the value of it is truly undefined. In ES5, undefined
 // can no longer be modified.

 // window and document are passed through as local variable rather than global
 // as this (slightly) quickens the resolution process and can be more efficiently
 // minified (especially when both are regularly referenced in your plugin).

为了获得额外的荣誉,请点击链接,您将了解为什么方法签名以分号开头。

另外,你的例子有点不正确:你不应该在调用函数时在最后一行传入'undefined'。我在上面粘贴的第一段中解释了原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 2016-10-17
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    相关资源
    最近更新 更多