Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.jQuery or jQuery?

(function($) {
// plugin code
})(window.jQuery);

Seems this code almost the same effect, as:

(function($) {
// plugin code
})(jQuery);

Should I use window.jQuery or jQuery for function argument? Does it make sense?

Same for Zepto, I've seen lots of people use window.Zepto in their plugins, but the code also works with just Zepto.

like image 208
Mark Avatar asked Dec 11 '22 16:12

Mark


2 Answers

There is no difference, window is the super global object in client-side JavaScript, all the functions and variables that are defined in the global context are methods and properties of the window object.

like image 157
undefined Avatar answered Dec 29 '22 20:12

undefined


It's the same just like $(document).ready(function(){..}) and $(function(){..}).

It's used to setting up a jQuery closure. The intention is to allow the variable $ to be used as a shortcut for jQuery without conflicting with other libraries and custom functions that also use $ as a variable name

This technique is often used by jQuery plugin authors to authorize their plugins. Check documentation for more infos.

like image 27
Eli Avatar answered Dec 29 '22 18:12

Eli