有没有什么时候
var result = !value ? null : value[0];
不会等同于
var result = value ? value[0] : null;
最佳答案
在此处将语言规范中的形式主义添加到 Pointy 的回答中。
当您通过 the conditional (? :) operator 运行代码时发生的第一件事是 ToBoolean
在左侧被调用。
当您执行 !value
时,在左侧调用 ToBoolean
,然后在左侧调用 ToBoolean。规范对这句话很清楚:
If ToBoolean(GetValue(lref)) is true, then
Let trueRef be the result of evaluating the first AssignmentExpression.
Else
Let falseRef be the result of evaluating the second AssignmentExpression.
因此,假设 JavaScript 引擎不包含错误,则在 JavaScript 中不会出现不同的结果。
关于javascript - 当 "!value ? null : value[0]"不等同于 "value ? value[0] : null"时,Javascript 中是否存在任何时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27787531/