admin管理员组文章数量:1391943
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
本文标签: specificationsJavaScript increment unary operator () on stringsStack Overflow
版权声明:本文标题:specifications - JavaScript increment unary operator (++) on strings - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744714182a2621302.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论