If comments are safe, then why not `x = 0; x + / * cmt * / +; `or` var f / * cmt * / oo = 'foo'; `work?

This thread has raised this question. Here are the code examples again. I am looking for an answer that accurately tells what is happening.

Both x = 0; x+/*cmt*/+;and var f/*cmt*/oo = 'foo';give syntax errors, which makes the answers in this wrong.

+3
source share
5 answers

From ECMAScript Link :

, , MultiLineComment , LineTerminator .

+2

. ++ foo - . , .

, , "".

+8
+5

. - , - . ++, .

+3

, , .

:

ax + ay - 0x01; /* hello */
^----^---------------------- Identifier (variables)
   ^----^------------------- Operator
          ^----------------- literal constant (int)
              ^------------- Statement separator
  ^-^--^-^---  ^------------ Whitespace (ignored)
                [_________]- Comments (ignored)

, :

identifier("ax");
operator("+");
identifier("ay");
operator("-");
const((int)0x01);
separator();

:

a/* hello */x + ay - 0x01;
^-----------^---^----------- Identifier (variables)
              ^----^-------- Operator
                     ^------ literal constant (int)
                         ^-- Statement separator
             ^-^--^-^------- Whitespace (ignored)
 [_________]---------------- Comments (ignored)

:

identifier("a");
identifier("x"); // Error: Unexpected identifier `x` at line whatever
operator("+");
identifier("ay");
operator("-");
const((int)0x01);
separator();

, .

, , , .

In fact, I recently just read an article about writing a simple JavaScript interpreter. It helped me with this answer. http://www.codeproject.com/Articles/345888/How-to-write-a-simple-interpreter-in-JavaScript

+1
source

All Articles