The ambiguity of the operator in the JS syntax is the comma "," of the ambiguity

Source: Internet
Author: User

See the "JS Language Essence and Programming practice" in the JS syntax explained in the ambiguity of the operator of the comma two semantics, make some notes let's take a look at these two examples:
Alert (eval (' a=; ')); /Example 1alert (eval (' a=1,2,3; ')); /Example 2
The result is that both will pop up 3, and this usage creates confusion because the comma "," can be either a syntactic delimiter or an operator. In Example 1 above, in Example 2, commas are used as "continuous operators". The parentheses force operator in Example 1, so its effect is to operate the following expression:
 
   
  
(a)
and assigns the result to the variable A. Since the expression is a continuous operation of three (direct) single-valued expressions, the result value is the last expression, which is the value 3. Whereas example 2 has no parentheses to enforce precedence, the assignment is done first by default priority, "variable A is assigned a value of 1", and example 2 after the assignment operation "A=1" is completed. Still continues to point to continuous operations, and the last expression is the direct Volume 3.
Here's a look at example 3:
 
   
  
A= (1, 2, (3,4,5), 6);//Example 3
Similarly, we can also know that the result of example 3 is to assign the variable a value:
 
   
  
[1,2,5,6]
This is because the expression "(3,4,5)" will be evaluated first and return the result value 5 as an element of the array declaration. In Example 3, it is emphasized that the comma here has the function of "syntax delimiter when array declaration" and "continuous operator" respectively. But if you like Example 4:
 
   
  
var a=1,2,3;//example 4
Thus, the comma is interpreted as the syntax delimiter used to separate multiple variables when the statement VAR declaration is declared, rather than as a continuous operator. While the statement var requires the "," number to separate the multiple identifiers, and the value 2 and the value 3 are clearly not a valid table only, so the code of Example 4 will be in the syntax of the explanation period to prompt an error.
There is an equally chaotic problem, and here is an example of 5:
 
   
  
Example 5. Displays the value of the last expression "value:240" Var I=100;alert ((i+=20,i*=2, ' value: ' +i));
Pop-up "value:240" because in the alert () function Call of this example there is also a pair of parentheses to represent the force operation. Because alert () is a function, so if you write:
 
   
  
Alert (i+=20,i*=2, ' value: ' +i);//Example 6
The alert () function will understand that three parameters are passed in. and the order of the function parameter table is from left to right, then example 6 this line of code will be interpreted as:
 
   
  
Alert (120,240, ' value:240 ');
But the alert () function itself only handles one parameter, so the final result will be the value "120" instead of the 240 we expected, but it needs to be added that after the execution of this line of code, the value of the variable i has actually become 240, in order for JS to understand the comma "," is a continuous operator, Instead of the syntax delimiter for the function parameter table, we add a pair of parentheses to the coercion (expression) operation here, which is example 5, so that because the outer parentheses are function callers, the internal
 
   
  
(i+=20,i*=2, ' value: ' +i)
is understood as a parameter and is an expression that needs to be evaluated. So the parentheses here are logically understood to be "force operators", the following code:
 
   
  
i+=20i*=2 ' Value: ' +i
is also used as an operand of this enforcement operator: An expression that is concatenated by ",". Until such a large circle was spared, the comma was properly interpreted as a "continuous operator".

The ambiguity of the operator in the JS syntax is the comma "," of the ambiguity

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.