你能解释一下这两个逻辑操作之间的区别吗:
let x = 1;
console.log('x = ' + x);
console.log('x === x++ : ' + (x === x++));
x = 1;
console.log('x = ' + x);
console.log('x++ === x : ' + (x++ === x));
最佳答案
后缀增量 (y = x++
) 确实会增加值,但会计算为之前的值。它几乎等于:
x² = x; // evaluate previous value of x
x += 1; // increase x
y = x²; // use previous value
因此 x === x++
等于:
// evaluate left side of ===
x¹ = x;
// evaluate right side
x² = x;
// ++
x += 1;
// comparison
x¹ === x² // true
是 x++ === x
是:
// evaluate left side
x¹ = x;
// ++
x += 1;
// evaluate right side
x² = x; // x was already incremented!
// comparison
x¹ === x²
关于javascript - 为什么在 Javascript 中 `x === x++` 为真而 `x++ === x` 为假?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59477674/