0

Possible Duplicate:
What is the purpose of this? (function ($) { //function code here })(jQuery);
What does the exclamation mark do before the function?
negating self invoking function? !function ($) { … }(window.jQuery);

!function ($) {
     $(function() {
            .
            .
            .
    })
}(window.jQuery)

I saw this in a jquery example and was wondering what the !function($){}(window.jQuery) was for. Particularly the exclamation mark was new to me.

Community
  • 1
  • 1
Stofke
  • 2,928
  • 2
  • 21
  • 28
  • 3
    So you found the answer on StackOverflow, and felt the need to post the fact that you found the answer on StackOverflow as a separate question and answer? – I Hate Lazy Dec 07 '12 at 20:47
  • It's not exactly a duplicate. This one combines the jQuery and the ! notation into one answer. Plus not everyone knows that !function($){}() and (function($){})() is basically doing the same thing. Also it wasn't until I typed exclamation mark that I found the answer. Some people will just try to find !function and that didn't turn up any results for me. – Stofke Dec 07 '12 at 20:51
  • 1
    @IHateLazy On the upside, I went and gave the original answers some rep... – canon Dec 07 '12 at 20:53
  • 1
    @Stofke: There's an infinite number of combinations of different questions that could be put together to make a "new" question. These have been asked and answered many times. They're two different questions, and both have been covered as you've shown. So yes, it's a duplicate. – I Hate Lazy Dec 07 '12 at 20:53
  • I just wanted to make it easier for people to find by just doing a search on the actual code. So far for trying to help people. I even added the references. – Stofke Dec 07 '12 at 20:57
  • 1
    To your credit, you found the answer on your own. But that should be an indicator that the answer can already be found. I'll bet there are *hundreds* of questions asking about this function syntax in various forms. The fact that `jQuery` is passed makes no difference. Should we have a new question for every time someone passes a different value into the function? – I Hate Lazy Dec 07 '12 at 20:59
  • No but I couldn't find a single answer via google or this site until I typed the word 'exclamation mark' because I was searching for !function. I was merely trying to save people from frustation. – Stofke Dec 07 '12 at 21:03
  • Reporting something as duplicate without reading it and using the links I provided myself now that's lazy. – Stofke Dec 07 '12 at 21:06
  • If you mean that there aren't any titles that use the `!`, there actually are [like this one](http://stackoverflow.com/questions/13597481/negating-self-invoking-function-function-window-jquery), but StackOverflow search doesn't work well for some characters, so the inclusion of `!` in yours won't do much better. – I Hate Lazy Dec 07 '12 at 21:06
  • You're calling me lazy? As I said, there are likely ***hundreds*** of questions that deal with this same thing. The fact that `window.jQuery` is passed doesn't make it a unique question. It's just one of many values that could be passed to the function. It is a duplicate, many times over, including [ones that pass `window.jQuery`](http://stackoverflow.com/questions/13597481/negating-self-invoking-function-function-window-jquery). – I Hate Lazy Dec 07 '12 at 21:08
  • Ok see it's a duplicate so is this one: http://stackoverflow.com/questions/10896749/what-does-function-function-window-jquery-do yet nobody commented on it when it was posted in june while the other examples are much older. So it's just arbitrary this closing of questions. Nevermind I will keep things to myself from now on instead of sharing and wasting my time. – Stofke Dec 07 '12 at 21:12
  • Yes, that's a duplicate too, and should be closed as such. But here's the difference. That person didn't know the answer and asked about it. You *knew* it was a dupe, and posted anyway. It's not arbitrary, but it does require people who are willing to give their time to help maintain the site to find the dupe, and close it. – I Hate Lazy Dec 07 '12 at 21:17
  • And you know how I found it by doing a search on !function ($) { }(window.jQuery). It's just stupid stackoverflow only searches for tags and titles and no full text searches. This in my opinion necessitates the posting of duplicates which are easier to find. I understand your point but it's not helping people. I don't give a ... about duplicates, the whole internet is filled with duplicate info it's about the ability to find the info you need what matters to me. And stackoverflow although I love it's existence doesn't do a good job at that imho. – Stofke Dec 07 '12 at 21:22
  • Fair enough. And yes, SO has its weak points. But if you make the decision to post dupes, don't be too surprised when people call you out on it and close the question. Putting the dupes in the answer will probably guarantee it to be closed. – I Hate Lazy Dec 07 '12 at 21:28
  • Guys, I believe this discussion belongs on [meta](http://meta.stackoverflow.com). Duplicates are complicated, we don't have good tools to find the best ones; it's hard to determine what should be considered a dupe of what (the timing should not be the first concern IMHO). Also, dupes have a purpose on the site (and aren't deleted for a reason). Related must-read: http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/. – bfavaretto Dec 07 '12 at 21:36
  • Thanks for the info. I won't do it from now on and save me the trouble. I console myself with the thought that someone will find the answer via my closed duplicate and leave it at that. – Stofke Dec 07 '12 at 21:41

1 Answers1

2

Ok I figured it out after doing some research but I will leave the answer in for others.

Basically it's to avoid conflicts with other javascript libraries which also use the dollarsign.

So Jquery advices to use jQuery or window.jQuery instead of the dollar sign. It's usually used in combination with jQuery.noConflict() to make jQuery give up on the $ sign used in other javascript libraries when you mix libraries.

This is actually a way of running a self-executing function that passes in the window.jQuery object and passes it on to the $ sign. This way you can keep using the dollar sign in your code without having to worry about other javascript libraries that could cause a conflict with your code.

Wrapping it up in this function saves yourself from using window.jQuery everywhere to replace the $ sign.

If you execute function () {}() like this it results in a syntax error as it's just a function declaration. The ! turns it into an expression which can be executed.

So basically you can use both for the same purpose.

!function($){}()
(function($){})()

Feel free to modify or complement.

references:

Community
  • 1
  • 1
Stofke
  • 2,928
  • 2
  • 21
  • 28