Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Function Declaration [duplicate]

I was taking a look through the bootstrap JS source and I came across something that I haven't seen before:

+function ($) { "use strict";
//...
}(window.jQuery);

What's the deal with the + in front of the function declaration? Is it to mitigate some potential minification problems or something?

I believe that placing a + before an expression type converts the result of the expression to a number, but I don't see what the relevance of that would be here.

Thanks for anybody who can shed some light on this for me.

like image 481
Adam Avatar asked Sep 22 '13 21:09

Adam


1 Answers

That is so that the function declaration is a function expression, so that it can be executed immediately.

Usually that is done by putting parentheses around it:

(function ($) { "use strict";
//...
}(window.jQuery));

or:

(function ($) { "use strict";
//...
})(window.jQuery);
like image 195
Guffa Avatar answered Sep 19 '22 02:09

Guffa