JavaScript中的好奇双var案例

发布于 2025-01-26 12:07:55 字数 379 浏览 0 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2025-02-02 12:07:55
  • V2初始化。
  • 表达式v ++增量v to 3,然后返回2
  • 语句var v = v ++;分配2 to v(覆盖3 in v 使用2V ++返回)。
  • console.log(v)打印2
  • v is initialized with 2.
  • The expression v++ increments v to 3 and then returns 2.
  • The statement var v = v++; assigns 2 to v (overwrites the 3 in v with the 2 returned from v++).
  • console.log(v) prints 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文