(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
.
(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
.
4 Answers
Reset to default 6There 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.
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.