-2

there is a "!" before function, why?

http://twitter.github.io/bootstrap/assets/js/bootstrap-transition.js

! function($) {

    //......

}(window.jQuery);
ActHtml
  • 27
  • 5
  • Forces the function to be an expression, where the ending `(window.jQuery)` then invokes it. – Joseph Apr 25 '13 at 01:45
  • 1
    also: http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function – tptcat Apr 25 '13 at 01:45
  • Just a visual clue to tell that's an IIFE, could as well be `~` or `+`. Unless a function returns something to a variable it won't matter. Most common practice is to wrap the whole expression in parentheses. – elclanrs Apr 25 '13 at 01:47
  • Asked many times already... please do some research before asking a question. This one should be closed. – plalx Apr 25 '13 at 01:51

1 Answers1

0

The ! causes the function to be treated as an expression, meaning it has the same effect as this:

(function($) {

    //......

})(window.jQuery);
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
  • 1
    Well, not quite :p If you want to return a value, then `(...)()` is much better than `!` – Niet the Dark Absol Apr 25 '13 at 01:46
  • It actually doesn't cause it to be evaluated... that's done by the parenthesis at the end. It is just a way of indicating that it is invoked immediately. It is for better readability (same as the parenthesis around the entire function) – Tom Pietrosanti Apr 25 '13 at 01:48