admin管理员组文章数量:1325236
I'd like to know exactly what's going on here. I know what $(document).ready(function() {...});
does and when it es into effect. Same goes for jQuery(function($) {...}
.
But what does this do?
!function ($) {
$(function(){
var $window = $(window)
//normal jquery stuff
})
}(window.jQuery)
Is it loaded when jQuery is loaded instead of when the document is 'ready'?
I'd like to know exactly what's going on here. I know what $(document).ready(function() {...});
does and when it es into effect. Same goes for jQuery(function($) {...}
.
But what does this do?
!function ($) {
$(function(){
var $window = $(window)
//normal jquery stuff
})
}(window.jQuery)
Is it loaded when jQuery is loaded instead of when the document is 'ready'?
Share Improve this question asked Jan 4, 2013 at 16:33 ArchonicArchonic 5,4026 gold badges42 silver badges57 bronze badges 3- My question is a bination of those within the context of jquery so I don't believe it's a duplicate. People will be looking specifically for this. – Archonic Commented Jan 4, 2013 at 18:50
- Reopen. This question is specific to jQuery, and doesn't mention exclamation marks. A careful reading reveals the exclamation mark is a red herring and that the asker is looking for an explanation of the jQuery parameters used in the outer function. – gilly3 Commented Jan 4, 2013 at 21:33
- I know the reference window.jQuery and thought the answers were spot on. – Archonic Commented Jan 7, 2013 at 14:36
5 Answers
Reset to default 8It creates a closure in which the variable $
is assigned the value of window.jQuery
.
The intention is to allow the uninformatively named variable $
to be used as a shortcut for jQuery
without conflicting with the large number of other libraries and custom functions that also use $
as a variable name.
Using the ! operator before the function causes it to be treated as an expression
!function () {}()
The syntax you're looking at is used for setting up a jQuery closure. This is used to ensure that the jQuery $
variable is garuanteed to be available and correct within the code; ie it can't be overwritten in the global scope by anything else (which is possible if you're using multiple libraries, etc).
This technique is often used by jQuery plugin authors -- if you're interested in finding out more, the docs are here, and explain in more detail why you'd want to wrap your jQuery code in a function like this.
The only point of interest that's different in your example is that in the docs, the function is wrapped in brackets, whereas in the example you've given it's preceded by a !
The !
is a not
operator, but doesn't actually get used for anything; I think it's just there instead of the brackets to save a single character of code. Probably helpful if you're into minifying javascript.
Not quite sure but I guess this is somewhat equivalent to (function(){})()
approach and it's about js closures. And it ensures $
and jQuery
are the same thing
The '!' is a 'not' operator. It doesn't do anything in the code. The only reason it is there is to signify that the function will execute immediately.
You may also see functions wrapped in parenthesis instead.
(function() {}());
Whatever is used is personal preference.
本文标签: javascriptWhat exactly does function ()(windowjQuery) doStack Overflow
版权声明:本文标题:javascript - What exactly does !function ($){...}(window.jQuery) do? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742167576a2426114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论