简体   繁体   English

在这种情况下,JavaScript如何评估运算符优先级?

[英]How does JavaScript evaluate operator precedence in this scenario?

I have below code and I just wanted to understand how the operator precedence working in this scenario. 我有下面的代码,我只是想了解运算符优先级在这种情况下的工作方式。 Just wanted to go back to old school and see how it works in JavaScript. 只是想回到老派,看看它如何在JavaScript中工作。

var num1 = 5,
    num2 = 10,
    result = (num1++)+num2;
    result1 = num1+++num2;
    result2 = (++num1)+num2;

console.log(result);
console.log(result1);
console.log(result2);

The above is printing as 以上是打印为

15 
16
18

respectively. 分别。

  1. Shouldn't result1 be throwing a syntax error? result1不应该result1语法错误吗?
  2. I did not get how result2 is 18 . 我没有得到result218 result2
result1 = num1+++num2;

is the same as 是相同的

result1 = (num1++) + num2;

So essentially, what is happening is: 所以从本质上讲,正在发生的是:

var num1 = 5,
    num2 = 10,
    result = (num1++) + num2; //5 + 10, then 5->6
    result1 = (num1++) + num2; //6 + 10, then 6->7
    result2 = (++num1) + num2; // 7->8, then 8 + 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM