正在使用&&;和| |至快捷方式typeof==”等;“未定义”;在javascript中检查是否稳定?

正在使用&&;和| |至快捷方式typeof==”等;“未定义”;在javascript中检查是否稳定?,javascript,Javascript,考虑以下嵌套数据结构: var options = { option1 = { option1a : "foo", option1b : "bar" }, option2 = {}, } 我有一个类似于上面的数据结构,我使用它作为函数输入的可选参数列表。因为所有这些都是可选输入,所以它可能存在,也可能不存在。最初,我使用typeof x==“undefined”语句链来确保,例如,选项、选项1和选项1a都已定义。在定义变量时,我的代码最终

考虑以下嵌套数据结构:

var options = {
    option1 = {
        option1a : "foo",
        option1b : "bar"
    },
    option2 = {},
}
我有一个类似于上面的数据结构,我使用它作为函数输入的可选参数列表。因为所有这些都是可选输入,所以它可能存在,也可能不存在。最初,我使用
typeof x==“undefined”
语句链来确保,例如,
选项
选项1
选项1a
都已定义。在定义变量时,我的代码最终会是这样的:

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语句?逻辑上,它们可以在某些实现中返回布尔值,但我对浏览器/javascript实现的了解还不够,无法确定地做出这样的声明。这是一种安全的方法,还是有更好的方法?

是的,这样可以正常工作
&
返回最后一个true语句或第一个false语句,
|
返回第一个true语句或最后一个false语句

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
还值得一提的是,falsy值包含的内容比JS中未定义的要多,因此您的两个检查并不完全相同。例如,值0将被视为虚假,即使它是“已定义的”

有关详细的逻辑规范,请参见官方规范

语法 语义学
是的,这是有保证的,也是规范的一部分。请注意,您的两种方法做的事情并不完全相同。您的第二个选项将保证没有未定义的
,但它也在测试没有任何项是错误的值(
false
null
NaN
0
等等)。如果您所要做的只是确保它们不是未定义的,那么它会起作用,但是如果
0
false
选项的有效值。选项1.option1a
,那么您的第二个选项可能并不是您想要的。
LogicalANDExpression :
BitwiseORExpression
LogicalANDExpression && BitwiseORExpression
LogicalANDExpressionNoIn :
BitwiseORExpressionNoIn
LogicalANDExpressionNoIn && BitwiseORExpressionNoIn
LogicalORExpression :
LogicalANDExpression
LogicalORExpression || LogicalANDExpression
LogicalORExpressionNoIn :
LogicalANDExpressionNoIn
LogicalORExpressionNoIn || LogicalANDExpressionNoIn
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.