Question

(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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

window.jQuery is object defined in window global object. window could be skipped. When skipped it is supposed to we window.jQuery. Most of people do not use window.jQuery rather use jQuery or simply $ and is more understandable also reduces the source code size.

window.jQuery = window.$ = jQuery;

You can read more over here.

There is a difference. !!window.jQuery will return false if jQuery is not defined while !!jQuery will throw an error if jQuery is not defined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top