开发者

Should I also assure undefined in a jQuery plugin closure?

开发者 https://www.devze.com 2023-04-06 22:03 出处:网络
I often see this described as the way to assure a safe environment for a jQuery plugin: (function($){ $.fn.myPlugin = function() {

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

精彩评论

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

关注公众号