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

javascript - What exactly does !function ($){...}(window.jQuery) do? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

5 Answers 5

Reset to default 8

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

发布评论

评论列表(0)

  1. 暂无评论