JavaScript中的void运算符将调用内部函数GetValue(expr),但始终返回undefined,无论值或表达式是什么.
规范说:
11.4.2无效算子
生产UnaryExpression:void UnaryExpression的计算方法如下:
- 设expr是评估UnaryExpression的结果.
- 打电话
GetValue(expr).- 返回undefined.
必须调用GetValue,即使它的值未被使用,因为它可能具有可观察到的副作用.
我的问题是,为什么?会发生什么样的可观察副作用?我们能否证明void如何改变程序流程,并讨论如果我们没有运行会发生什么GetValue?
只是执行codecademy课程,当我运行代码时,一切都有效,除了这个if/else语句.当它到达那个时,无论我为userAnswer提示输入什么,我只是得到'if'输出.有人可以解释原因吗?一直在读这个东西100x,答案不是跳出来的.万分感谢.
var userAnswer = prompt("Do you want to race Bieber on stage?");
if(userAnswer="yes") {
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!")
} else {
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'")
}
Run Code Online (Sandbox Code Playgroud) javascript ×2