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.
精彩评论