最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.jQuery or jQuery? - Stack Overflow

programmeradmin5浏览0评论
(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.

Share Improve this question asked Apr 6, 2013 at 8:23 MarkMark 2453 silver badges11 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

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.

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.

发布评论

评论列表(0)

  1. 暂无评论