在jQuery插件中传递params意味着什么?

时间:2013-02-08 15:19:00

标签: javascript jquery jquery-plugins jquery-events

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

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

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

      $.fn.pluginname= function(options) {      

        //Code

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

1 个答案:

答案 0 :(得分:4)

来自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'。我在上面粘贴的第一段中解释了推理。