How should formatting closures?

I ran the script through JSLint and selected a specific problem with the placement in parentheses.

I wrote:

(function(){})(); 

And it was suggested to use:

 (function(){}()); 

I am curious what errors or problems this fix resolves. I would suggest that since JSLint chose this as a problem, there must be a problem for someone.

Extended forms:

 ( function (p) { ...code... } )(param); //parameters after the parens 

-vs -

 ( function (p) { ...code... }(param) //parameters within the parens ); 
+6
source share
2 answers

The specific issue that JSLint is trying to fix is ​​the lack of closure ; , which may cause an error when the function is interpreted as an argument:

 (function A( arg ){ // stuff }) (function B(){ ... }); 

That's right, B is passed to A as arg . However, this is often not the intended case, as they are often designed for self-fulfillment, and the rear () been forgotten. The proposed syntax eliminates any confusion that you may have accidentally forgot to perform your function as intended.

For what it's worth, I almost always use the first syntax; habit.

+5
source

According to Crockford at http://www.yuiblog.com/crockford/ (I can’t remember which video, but I think this is the beginning), this is purely style to help facilitate reading and is not related to errors or problems.

Edit:

I think this is in Act III: Ultimate Function

0
source

Source: https://habr.com/ru/post/894142/


All Articles