parsing JavaScript parentheses "()" with the Polysemy _javascript tips

Source: Internet
Author: User

JavaScript parentheses have five kinds of semantics

Semantic 1, Function declaration time parameter table

Copy Code code as follows:

function func (ARG1,ARG2) {
// ...
}

Semantic 2, used in conjunction with some statements to achieve certain limitations
Copy Code code as follows:

Use with for in
For (var a in obj) {
// ...
}

And if used together
if (boo) {
//...
}

Use with the while
while (Boo) {
// ...
}

Use with Do While
do{
// ...
}while (Boo)

Note: When used with an if, while, and do, the parentheses convert the result of the expression into a Boolean value implicitly. See implicit type conversions in JavaScript.

Semantics 3, used with new to pass values (arguments)

Copy Code code as follows:

Suppose the class person is already defined, it has two field names (name), age
var p1 = new Person (' Jack ', 26);

Semantic 4, as the invocation operator of a function or object method (if a parameter is defined, it can also be passed as Semantic 3)
Copy Code code as follows:

Suppose you've defined a function func
Func ();

Suppose object obj has been defined and has the Func method
Obj.func ();

Here to mention the typeof operator, some people like to use this

typeof (XXX);

Note that the typeof parenthesis is not semantic 4 (that is, not a function call), but rather the semantic 5 that follows. I use typeof generally without the following parentheses.

Semantic 5, forcing expression operations

The most familiar thing about semantic 5 is using Eval to parse JSON

Copy Code code as follows:

function Strtojson (str) {
Mandatory operator () on both sides of the string in eval
var json = eval (' (' + str + ') ');
return JSON;
}

And if you use more of the anonymous function self execution
Copy Code code as follows:

(function () {
// ...
})();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.