4

I was just being goofy and analyzing some code in the source of some sites and I ran across this convention for an anonymous function.

!function(window){
     //so a bunch of stuff
}(this)

I have never seen the !function usage for an anonymous function. What does it do, and why would someone use it?

UPDATE2: my apologies, after additional testing, the following thread pretty much sums up its usage:

What does the exclamation mark do before the function?

Community
  • 1
  • 1
james emanon
  • 11,185
  • 11
  • 56
  • 97
  • dang, you are right. I did a search for various things like "!function" and nothing came up. thanks. – james emanon Dec 21 '13 at 21:12
  • 1
    Wha, how can you **update** your question if you asked it 2 minutes ago? – Tim Seguine Dec 21 '13 at 21:12
  • well, meaning, before I posted I was still testing.. then figured some reason for it and decided to post it anyways figuring it might help someone else. So "I did" Have a question but I sorta figured out why, and posted anyways for future. – james emanon Dec 21 '13 at 21:13
  • 1
    Your conclusion is wrong, by the way. It doesn't matter if `b` is defined or not. – JJJ Dec 21 '13 at 21:14
  • It is better than the syntax convention douglas crawford calls "dog balls" – Tim Seguine Dec 21 '13 at 21:15
  • 2
    `!function(input){}(foo)` gives me foo is not defined. `(function(input){})(foo)` does also – Tim Seguine Dec 21 '13 at 21:19
  • ok. browser weirdness. i am getting error now too, and before I wasn't. must of been careless. my bad. – james emanon Dec 21 '13 at 21:26

0 Answers0