Java Reference
In-Depth Information
The parser keeps parsing the source code until it finds an offending token. It does not
always insert a semicolon before a line terminator. Consider the following code:
var x
x
=
200
printf("x = %d", x)
The parser will treat the three lines of source code (second to fourth) as one
assignment statement (x = 200) and insert a semicolon after 200. The transformed code
will be as follows:
var x;
x
=
200;
printf("x = %d", x);
Consider the following code that prints 20:
var x = 200, y = 200, z
z = Math.sqrt
(x + y).toString()
print(z)
The parser does not insert a semicolon at the end of the third line ( z = Math.sqrt ).
It thinks that the ( in the following line is the start of the argument list for a function Math.
sqrt . The transformed code looks as follows:
var x = 200, y = 200, z;
z = Math.sqrt
(x + y).toString();
print(z);
However, the writer of the code might have meant to assign the function reference
Math.sqrt to the variable named z . In that case, the automatic insertion of semicolons
changes the intended meaning of the code. If you insert the semicolon yourself after the
assignment statement, the code output is different:
var x = 200, y = 200, z;
z = Math.sqrt; // Assigns Math.sqrt function reference to z
(x + y).toString(); // Converts x + y to a string and ignores the result
print(z);
function sqrt() { [native code] }
 
Search WWH ::




Custom Search