开发者

Should I also assure undefined in a jQuery plugin closure?

I often see this described as the way to assure a safe environment for a jQuery plugin:

(function($){
  $.fn.myPlugin = function() {
    ...
  };
})(jQuery);

Wouldn't it be better to also safeguard undefined, like so:

(function($, undefined){
  $.fn.myPlugin = function() {
    ...
  };
})(jQuery开发者_运维问答);

Or does it not matter? and if so, why?


It is probably not necessary. If you test whether a variable is undefined, you should prefer using

typeof variable === 'undefined'

anyway.

"Safeguarding" jQuery on the other hand is something you definitely should do, otherwise your plugin will not work if jQuery is used in noConflict mode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜