+
is a shortcut, a character of two ()
, which would be the easiest way to read to invoke a function (...a declaração da função..)()
.
The +
will force the next part to the operator to be interpreted as an expression, ie a function statement and will cause the function to run once it is followed by ()
.
+function(m) {
console.log('Olá ' + m + '!');
}('mundo')
Those kind of functions are called IIFE, and this kind of shortened syntax can be done with the converter/operator +
, or denial !
or bitwise ~
. The idea is to save characters.
!function() {console.log('Usando !');}();
~function() {console.log('Usando ~');}();
-function() {console.log('Usando -');}();
(function() {console.log('Usando ()');})();
Personally I think (function...)()
, or the variant (function(){}())
, is clearer, and therefore the right way. Therefore not using other operators that have as a function something else, but that as a side effect make the browser to interpret the code and save 1 character.
Related to Soen: https://stackoverflow.com/q/13341698/1452488
– Woss