Abstract: This article mainly introduces the five kinds of semantics of javascript small brackets.
JavaScript small brackets have five semantics
Semantics 1, Parameter table when function declaration
function func (arg1,arg2) { //...}
Semantics 2, combined with some statements to achieve certain qualifiers
With for in use for (Var a in obj) { //...} Use if with if (boo) { //...} Use while (boo) { //...} with while Use do{ //...} with Do and 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 has been defined, it has two field names (name), age var p1 = new Person (' Jack ', 26);
Semantics 4, as a call operator for 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 Funcfunc () has been defined,//assume that the object obj has been defined, and have 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 multiple invocation methods for named functions
Semantics 5, forcing expression operations
As for semantic 5, the most familiar is the use of eval to parse JSON
function Strtojson (str) { ///eval strings are added with the Force operator () var json = eval (' (' + str + ') '); return JSON;}
Another example is the use of anonymous function self-executing
(function () { //...}) ();
Note that the 1th pair of parentheses in the above code is semantics 5, while the 3rd pair is semantic 4.
Author: snandy original link
Ambiguity of the JavaScript parenthesis "()"