Sign + before function in Jquery? what does it mean?

Asked

Viewed 143 times

6

I will finish a system started by another colleague who was disconnected from the company and I am in doubt about the use of + before starting the function, and I would like to know the impact of this assignment, follows:

+function ($) {

//funções dentro do arquivo .js
//e termina dessa forma

}(jQuery);

I’m used to working two ways;

  1. $(function ($) {...})
  2. $(document).ready( function() {...})
  • 2

    I’ve seen it answered here.

1 answer

4


The + in front of the function serves to execute it immediately IIFE (Immediately Invoked Function Expression). If there is no such operator or any other way of indicating that the function will be executed immediately the declaration will not be considered as an expression by parser browser.

In the case of your example you are running the function right after setting it and pass as parameter the variable JQuery. In the case of your first example, you can also use other syntax, such as the following:

(function ($) {

//funções dentro do arquivo .js
//e termina dessa forma

})(jQuery);

You can use any unary operator to have the same result:

!function(){ /* implementação */ }();
~function(){ /* implementação */ }();
-function(){ /* implementação */ }();
+function(){ /* implementação */ }();
  • The - can also be used? there inversely?

  • @Wagnerfernandomomesso added the answer to that in the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.