Ambiguity of JavaScript braces "{}"

Source: Internet
Author: User

Brackets in JavaScript have four meanings.

Semantic 1: Organizes compound statements, which is the most common

 
 
  1. if( condition ) {  
  2.   //...  
  3. }else {  
  4.   //...  
  5. }  
  6. for() {  
  7.   //...  

Semantics 2: Object Direct Volume Declaration

 
 
  1. var obj = {  
  2.     name : 'jack',  
  3.     age : 23  
  4. }; 

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

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

 
 
  1. function f1(){  
  2.     //...  
  3. }  
  4.  
  5. var f2 = function(){  
  6.     //...  

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 structured exception handling

 
 
  1. try {  
  2.     //...  
  3. }catch( ex ){  
  4.     //...  
  5. }finally{  
  6.     //...  

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 is even N long

 
 
  1. Function () {} () // immediate execution of anonymous functions. syntax analysis reports
  2. {}. Constructor // The constructor that gets the direct object 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, no error will be reported if a variable is added for receiving.

The same situation is shown in figure

Var fn = function () {} (), and no error is reported.

In fact, js statements take precedence. 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 that the object member is obtained. Naturally, the vertex operator following the object can be executed normally.
 

Link: http://www.cnblogs.com/snandy/archive/2011/02/28/1966894.html

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.