Home  >  Article  >  Web Front-end  >  Summary of usage of brackets () in javascript_Basic knowledge

Summary of usage of brackets () in javascript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:52:211231browse

1. Increase priority

(1 2)*3 Consistent with mathematical operations, first calculate 1 2 and then multiply by 3 to get 9

can also be other expressions, such as:

Copy code The code is as follows:
(a (function(i){return i}(2)))*c

2. The parameters of the function should be placed in brackets ()

Copy the code The code is as follows:
function fun(a,b,c)
{
//…
}

3. Immediately execute the function expression
Copy code The code is as follows:

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

The parameters in the brackets (1,2,3) here correspond to the parameters of the previous function. When the first bracket When the code conforms to the expression rules, the previous code will be executed as a function expression, so it is best to separate it with ";" in front of the first function expression, otherwise it will report that the value of the previous expression is not Function error.

For example: alert(1)(function(){})(), at this time alert(1) is executed first. Since it complies with the function expression rules for immediate execution, the return value of alert(1) will be used as a function , and the value in the latter bracket is passed in as a parameter, but alert(1) returns undefined, so an error will be reported. The solution is to add ";" or "," after alert(1) to split it into two expressions.

You can also use

to copy the code The code is as follows:

(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)

and other methods, as long as the function complies with the syntax rules of function expressions.

When executing a function alone, parentheses are also required and cannot be omitted, such as: fun(), fun(1,2,3)

4. Execute single or multiple expressions and return the value of the last expression. Multiple expressions need to be separated by commas ","

Copy code The code is as follows:
(1,2 3,4 5,6)//The code will be executed once, and finally 6 As return value

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

Copy code The code is as follows:
if(a b==c){} //The content between if and { needs to be placed in brackets

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn