admin管理员组文章数量:1245105
(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.
本文标签: javascriptwindowjQuery or jQueryStack Overflow
版权声明:本文标题:javascript - window.jQuery or jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740171035a2235549.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论