Ambiguity of the JavaScript parenthesis "()"

Source: Internet
Author: User

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

Related Article

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.