The general way to write an immediate execution function is (() = > {}) (). See someone write this (0, () = > {}) (),). What are the advantages of this latter way of writing?

immediate execution function is generally written as (() = > {}) (). Seeing someone write this (0, () = > {}) (),), would you like to ask what are the advantages of this latter way of writing? Thank you!

Jun.12,2021

you can also write

(true,"",3.1415926,[""],new Map(),()=>{console.log("")})()

seriously, the js comma operator executes the expression sequentially and gets the value of the rightmost expression

clipboard.png

benefits? < del > may be able to make people limp < / del > (by the way, execute the expression before the last comma)


there is no difference between good and bad. The point is why it is written in this way

.

in js, the function declaration is advanced, so adding () immediately after the function declaration throws a syntax error

functuion (){}()
//Uncaught SyntaxError: Unexpected token {

so we want the function to be processed in the position of the code, so we can turn the function into a function expression. Here are a few examples

(function(){})()

(function(){}())

+function(){}()

-function(){}()

~function(){}()

var a = function(){}()

the first way of writing above is the same as the two ways mentioned in your question. Why do you say that the second one you mentioned is the same as the first one?

because the expression has a return value, the return value is its result

var a,b;//js,
a = 1; //1
a = (1,2);//2,,,2
Menu