Parenthesized of usage summary in javascript

  • 2020-03-30 02:39:07
  • OfStack

1. Increase your priorities

1 plus 2 times 3 is the same thing as we did in math, so we're going to multiply 1 plus 2 times 3 to get 9

Also can be other expressions, such as:

(a+(function(i){return i}(2)))*c

2. The parameters of the function should be put in parentheses ().

function fun(a,b,c)
{
    // ... 
}

3. Execute the function expression immediately

(function fun(a,b,c)
{
    // ... 
})(1,2,3)

The arguments in the parentheses (1,2,3) correspond to the arguments in the previous function. When the code in the first bracket conforms to the expression rule, the previous code will be executed as a function expression, so it is better to put "; "before the first function expression. Otherwise, the value of the previous expression is not a function error.

For example: alert(1)(function(){})(), where alert(1) is executed first, the return value of alert(1) is passed in as a function and the value in the latter bracket is passed in as an argument because it conforms to the rule of function expression that is executed immediately, but alert(1) returns undefined, so an error is reported. The solution is to put a "; "after alert(1). Or ", "split it into two expressions.

Immediate execution is also useful


(function fun(a,b,c)
{
    // ... 
}(1,2,3))

!function fun(a,b,c)
{
    // ... 
}(1,2,3)

void function fun(a,b,c)
{
    // ... 
}(1,2,3)

As long as the function conforms to the function expression syntax rules.

When a single function is executed, parenthesis is also needed, and cannot be omitted, such as: fun(),fun(1,2,3)

Execute a single or multiple expressions , and returns the value of the last expression separated by the comma ","

(1,2+3,4+5,6)// The code will be executed once and finally 6 As the return value 

5. Conditional expressions , similar to 4, but used in conditional judgment

if(a+b==c){} //if  and  {  The contents between the two should be put in brackets 

 


Related articles: