2

I'd like to know exactly what's going on here. I know what $(document).ready(function() {...}); does and when it comes 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'?

3
  • My question is a combination of those within the context of jquery so I don't believe it's a duplicate. People will be looking specifically for this. 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. Commented Jan 4, 2013 at 21:33
  • I know the reference window.jQuery and thought the answers were spot on. Commented Jan 7, 2013 at 14:36

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I usually do (function(a,b){}(p1, p2)), I guess !function(a,b){}(p1,p2) is just less characters.
I think question also goes for ! before the function body
To complete the answer, the ! at the beginning is a convention used by some programmers to indicate that the function is invoked immediately. You could also use +. It does not affect the way that the code runs in this case (it would if you were assigning the function to a variable or doing anything else with it - it has the effect of turning the return value into a boolean).
So that's why my $ is undefined error in my page head disappeared :) awesome answers guys!
@iblamefish - Your comment makes it sound like the ! is optional and can safely be omitted. The ! turns what would be a function declaration into a function expression. Without it you would get a runtime error. I think your point is that there's more than one way to do it, which is certainly true. But, to say it's merely a "convention" that "does not affect the way that the code runs" is potentially confusing.
4

Using the ! operator before the function causes it to be treated as an expression

!function () {}() 

2 Comments

I didn't exactly get what being treated as an expression meant but others' say it has the code execute immediately.
! makes function an expression but still doesn't invoke the function. In the example we are invoking function by adding () at the end. () has higher precedence than !.
3

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.

Comments

0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.