Javascript Small brackets have 5 semantics
Semantic 1: Parameter table when function declaration
1 function func (arg1, arg2) { 2 // ... 3
Semantic 2: Used in conjunction with some statements to achieve certain qualifiers
1 //use with for2 for(varAinchobj) { 3 // ... 4 } 5 6 //use with If7 if(boo) {8 //... 9 } Ten One //working with the while A while(boo) { - // ... - } the - //use with Do and - Do{ - // ... +} while(Boo)
Note: When used with the IF, while, and do, the parentheses implicitly convert the result of the expression into a Boolean value. See Implicit type conversions everywhere .
semantics 3: Used with new to pass values (arguments)
// assuming that the class person is already defined, it has two field names (name), age var New Person (' Jack ', 26);
Semantics 4: The invocation operator as a function or an object method (if a parameter is defined, it can be passed as an argument with semantic 3)
// assume that the function func has been defined func (); // assume that the object obj is already defined and has the func method Obj.func ();
The typeof operator is mentioned here, and some people like to use it:
typeof (XXX);
Note that the parentheses after typeof are not semantic 4 (that is, not function calls), but rather the semantics 5 that are mentioned later. I use typeof generally without the parentheses in the back.
See also four ways to call named functions.
Semantics 5: Enforcing expression operations
As for semantic 5, the most familiar is the use of eval to parse JSON
1 function Strtojson (str) { 2 // eval string with mandatory operator () 3 var json = eval (' (' + str + ') '); 4 return json; 5
Another example is the use of anonymous function self-executing
1 (function() { 2 // ... 3 }) ();
Note that the first pair of parentheses in the above code is semantic 5, while the second pair is semantic 4.
[to] the ambiguity of the parentheses "()" in Javascript