There are five semantics of parentheses in Javascript
Semantics 1, parameter list when function is declared
function func(arg1,arg2){
// ...
}
Semantic 2, used in combination with some statements to achieve certain restrictions Function
// Use with for in
for(var a in obj){
// ...
}
// Used with if
if(boo){
//...
}
// Used with while
while(boo){
// ...
}
// Used with do while
do{
// ...
}while(boo)
Note: When used with if, while and do while, parentheses will replace the expression in it The result is implicitly converted to a Boolean value. See Implicit Type Conversions in JavaScript.
Semantic 3, used with new to pass values (actual parameters)
// Assume that the class Person has been defined, which has two fields name (name) and age (age)
var p1 = new Person('Jack',26) ;
Semantic 4, as a call operator for functions or object methods (if parameters are defined, actual parameters can also be passed in the same way as Semantic 3)
// Assume that the function func has been defined
func();
// Assume that the object obj has been defined and has the func method
obj.func();
Here is the typeof operator, some people like to use it like this
typeof(xxx);
Please note that the parentheses after typeof are not semantic 4 (that is, not a function call), but semantic 5 mentioned later. I usually use typeof without the following parentheses.
Semantics 5, forced expression evaluation
Regarding Semantic 5, everyone is most familiar with using eval to parse JSON
function strToJson(str){
// Force operators () are added on both sides of the string in eval
var json = eval('(' str ')');
return json ;
}
Another example is the anonymous function self-execution
(function(){
// ...
})();