JavaScript中的好奇双var案例
在JavaScript中奇怪的案件,您并不完全是人们期望的
function test() {
var v = 2;
var v = v++;
console.log(v);
}
test();
为什么在这里似乎忽略了++
?在什么时候执行++操作?
“ V ++增量V到3并返回2” 第一个,增量或返回是什么?
- 如果首先增加,则返回的值应为“ 3”,
- 如果它返回了第2个,则在此之后进行增量,因此增量值为2,V为3 ...
Curious case in JavaScript where you get not exactly the most people would expect
function test() {
var v = 2;
var v = v++;
console.log(v);
}
test();
Why the ++
seems ignored here? at what point the ++ operation is executed?
"v++ increments v to 3 and returns 2"
what is the first, increment or return?
- if it increments first, the returned value should be "3"
- if it returns first 2, then after that increments, so the incremented value is 2 and v should be 3...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
V
用2
初始化。v ++
增量v
to3
,然后返回2
。var v = v ++;
分配2
tov
(覆盖3
inv 使用
2
从V ++
返回)。console.log(v)
打印2
v
is initialized with2
.v++
incrementsv
to3
and then returns2
.var v = v++;
assigns2
tov
(overwrites the3
inv
with the2
returned fromv++
).console.log(v)
prints2