I am on Chromium Version 53.0.2785.143 Built on Ubuntu, running on Ubuntu 16.04 (64-bit)
According to the ECMAScript® Language Specification, prefix increment operator is evaluated as follows:
With this in mind, I cannot explain this result:
++'1';
> Uncaught ReferenceError: Invalid left-hand side expression in prefix operation
when the following code works like a charm:
var x = '1';
++x;
> 2
As far as I understand, in both cases the first 3 bullet points of the second step are true, whereas for ++'1'
case the fourth bullet is also true (but why?) and for the ++x
case it is false, raising no error. Am I right?
PS: Firefox throws a SyntaxError: invalid increment operand
instead of a ReferenceError
I am on Chromium Version 53.0.2785.143 Built on Ubuntu, running on Ubuntu 16.04 (64-bit)
According to the ECMAScript® Language Specification, prefix increment operator is evaluated as follows:
With this in mind, I cannot explain this result:
++'1';
> Uncaught ReferenceError: Invalid left-hand side expression in prefix operation
when the following code works like a charm:
var x = '1';
++x;
> 2
As far as I understand, in both cases the first 3 bullet points of the second step are true, whereas for ++'1'
case the fourth bullet is also true (but why?) and for the ++x
case it is false, raising no error. Am I right?
PS: Firefox throws a SyntaxError: invalid increment operand
instead of a ReferenceError
- 1 You get the same error if you do ++1, by the way – Joseph Young Commented Oct 12, 2016 at 13:58
-
1
PutValue('1', 2)
throws an error because it cannot assign to a string literal. You need a variable or some other kind ofReference
. – Bergi Commented Oct 12, 2016 at 13:59
2 Answers
Reset to default 10The problem is that your ++
operator implicitly involves an assignment, and you can't assign a new value to a string constant. Note that
++2;
is also erroneous for the same reason.
In my understanding, ++
is similar to += 1
.
So it will work for ++x
as it will be evaluated to x+=1
or x=x+1
, but ++'1'
is a string literal and does not has left hand side value to assign, hence it fails
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744714183a4589517.html
评论列表(0条)