javascript - 正在使用 && 和 ||快捷方式 typeof == "undefined"在 javascript 中检查稳定?

标签 javascript

考虑以下嵌套数据结构:

var options = {
    option1 = {
        option1a : "foo",
        option1b : "bar"
    },
    option2 = {},
}

我有一个与上面类似的数据结构,我将其用作函数输入的可选参数列表。由于所有这些都是可选输入,因此它可能存在也可能不存在。最初,我使用 typeof x == "undefined" 语句链来确保,例如,optionsoption1option1a 都定义好了。我的代码最终会像这样定义一个变量:

if(typeof option != "undefined" 
  && typeof option.option1 != "undefined" 
  && typeof option.option1.option1a != "undefined){
    var x = option.option1.option1a;
} else {
    var x = "default";
};

我注意到我可以将其缩短为以下内容:

var x = option && option.option1 && option.option1.option1a || "default";

&& 是否始终返回最后一个 true 或第一个 false 语句? || 是否始终返回第一个 true 语句?从逻辑上讲,他们可以在某些实现中返回 bool 值,但我对浏览器/javascript 实现的了解还不够明确,无法明确做出该声明。这是执行此操作的安全方法,还是有更好的方法?

最佳答案

是的,这会正常工作。 && 返回最后一个真语句或第一个假语句,|| 返回第一个真语句或最后一个假语句。

var a = false || 0 // a === 0
var b = false && 0 // b === false
var c = true || 1 // c === true
var d = true && 1 // d === 1

并且 &&|| 之前求值,所以

var e = false || 0 && null // e === null
var f = 1 || 2 && 3 // f === 1

还值得一提的是,虚假值包含的不仅仅是 JS 中的未定义值,因此您的 2 个检查并不完全相同。例如,值为 0 将被认为是虚假的,即使它是“定义的”

查看官方规范 here用于详细的逻辑规范。

Syntax

LogicalANDExpression :
BitwiseORExpression
LogicalANDExpression && BitwiseORExpression
LogicalANDExpressionNoIn :
BitwiseORExpressionNoIn
LogicalANDExpressionNoIn && BitwiseORExpressionNoIn
LogicalORExpression :
LogicalANDExpression
LogicalORExpression || LogicalANDExpression
LogicalORExpressionNoIn :
LogicalANDExpressionNoIn
LogicalORExpressionNoIn || LogicalANDExpressionNoIn

Semantics

The production LogicalANDExpression : LogicalANDExpression && BitwiseORExpression is evaluated as follows:
Let lref be the result of evaluating LogicalANDExpression.
Let lval be GetValue(lref).
If ToBoolean(lval) is false, return lval.
Let rref be the result of evaluating BitwiseORExpression.
Return GetValue(rref).
The production LogicalORExpression : LogicalORExpression || LogicalANDExpression is evaluated as follows:
Let lref be the result of evaluating LogicalORExpression.
Let lval be GetValue(lref).
If ToBoolean(lval) is true, return lval.
Let rref be the result of evaluating LogicalANDExpression.
Return GetValue(rref).
The LogicalANDExpressionNoIn and LogicalORExpressionNoIn productions are evaluated in the same manner as the LogicalANDExpression and LogicalORExpression productions except that the contained LogicalANDExpressionNoIn, BitwiseORExpressionNoIn and LogicalORExpressionNoIn are evaluated instead of the contained LogicalANDExpression, BitwiseORExpression and LogicalORExpression, respectively.
**NOTE** The value produced by a && or || operator is not necessarily of type Boolean. The value produced will always be the value of one of the two operand expressions.

关于javascript - 正在使用 && 和 ||快捷方式 typeof == "undefined"在 javascript 中检查稳定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18492916/

相关文章:

javascript - 使用 JavaScript 修改 Facebook 共享内容

javascript - 服务器上出现意外的 T_STRING 错误,但本地主机上没有

javascript - 无法返回 meteor 中的默认模板

javascript - 如何使用状态代码 200、404、300(jquery 如何在内部完成和失败工作)

javascript - Express.js 查看 "globals"

c# - 执行我的 javascript 后页面重新加载,我不希望它这样做

javascript - 将 CSS 添加到 Highcharts 中特定系列的图例标签

javascript - 注销并按后退按钮后,同一页面将在 PHP 中显示

javascript - Chrome 中的慢 cors 预检选项请求

javascript - 使用浏览器内 JS 以数值方式求解三 Angular 方程