Semantics of braces in JavaScript

Source: Internet
Author: User

The braces "{}" in Javascript have four meanings:

Semantics 1. Organize compound statements, which are the most common:

if( condition ) {  //...}else {  //...}for() {  //...}

Syntax 2: Object Direct Volume declaration:

var obj = {name : 'jack',age : 23};

It is a value assignment statement, where {name: 'jack', age: 23} is an expression.

Semantics 3: Declares the direct quantity of a function or function:

function f1(){//...}var f2 = function(){//...}

The difference between f1 and f2 is that the former is in the syntax interpretation period and the latter is in the runtime period. The difference is: if the code that calls this function is defined after the function, there is no difference. If the code that calls this function can still be called by f1 before the function is defined, f2 will report an error, the prompt f2 is undefined.

Syntax 4: syntax symbols for handling structured exceptions:

try {//...}catch( ex ){//...}finally{//...}

The braces here are different from the conforming Statement (semantic 1). if there is only one statement in the braces, the braces such as if/else/for can be omitted, however, try/catch/finally cannot be omitted.

The following code gets stuck with N for a long time:

Function () {} () // The anonymous function is executed immediately. During the syntax analysis period, {}. constructor is reported. // The constructor obtains the constructor to obtain the object's direct quantity. An error is reported during the syntax analysis period.

What is puzzling is why []. constructor does not report an error when writing this code. One is a constructor that wants to get the direct amount of objects, and the other is a constructor that gets the direct amount of arrays.

Of course, adding a variable to receive will not report an error, var c = {}. constructor;

In the same case: var fn = function () {} (), no error is reported.

In fact, js statements are "statement first", that is, {} is interpreted as a composite statement block (semantic 1) rather than a direct object volume (semantic 2) or a declared function (semantic 3).

Function () {} (), braces are understood as compound statements. The syntax of the previously declared function () is incomplete, which leads to errors in the syntax analysis period. {}. Constructor. Braces are considered as compound statements. Braces are followed by dot operators. If there are no reasonable objects before the dot operators, an error is also reported.

Solution: Add a forced operator ()

(Function () {}) (), (function () {}); // force it to be understood as a function (semantic 3), and "function ()" indicates executing the function, the statement is executed immediately. ({}). Constructor // ({}) forces braces to be understood as the object's direct quantity (meaning 2), "object. xx "indicates getting the object members. Naturally, the vertex operator can be executed normally.

Function () {}(); the cause of syntax error is irrelevant to (); function call operators.

Essentially, function () {} groups tokens Based on the lexical analysis period. Function is regarded as the first token element of the start position of the ExpressionStatement. This is not allowed by emca262. The reason for disallow is explicit, that is, the ambiguity between function expressions and function declarations. You can understand that the function keyword can never be at the top of an ExpressionStaement.

Let's see the value assignment statement f = function (){};

f : LeftHandSideExpression= : AssignmentOperator

Function () {}; is considered as the value assignment expression of the entire statement, that is, AssignmentExpression. Therefore, he passed the syntax check reasonably and legally and became a function expression. FunctionExpression.

So at this time, even if you f = function () {} (); is also legal in syntax.

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.