简体   繁体   English

正在使用&&和||快捷键typeof ==“undefined”在javascript中检查稳定?

[英]Is using && and || to shortcut typeof == “undefined” checks stable in javascript?

Consider the following nested data structure: 考虑以下嵌套数据结构:

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

I've got a data structure like the one above, which I'm using as a list of optional parameters for inputs to a function. 我有一个像上面那样的数据结构,我正在使用它作为函数输入的可选参数列表。 Since all of this is optional inputs, it may or may not exist. 由于所有这些都是可选输入,因此可能存在也可能不存在。 Initially, I was using chains of typeof x == "undefined" statements to make sure that, for example, options , option1 , and option1a are all defined. 最初,我使用了typeof x == "undefined"语句的链,以确保例如, optionsoption1option1a都已定义。 My code would end up looking like this for defining a variable: 我的代码最终看起来像这样定义一个变量:

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

I noticed that I can shorten this to the following: 我注意到我可以将其缩短为以下内容:

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

Does && consistently return the last true or first false statement? &&是否始终返回最后的真实或第一个错误陈述? And does || 并且|| consistently return the first true statement? 一贯回归第一个真实的陈述? Logic would dictate that they could return booleans in some implementations, but I don't know enough about browser / javascript implementations to make that statement definitively. 逻辑将要求他们可以在某些实现中返回布尔值,但我不太了解浏览器/ javascript实现以明确地做出该语句。 Is this a safe way to go about doing this, or is there a better way? 这是一种安全的方式来做这件事,还是有更好的方法?

Yes that will work correctly. 是的,这将正常工作。 && returns the last true statement or first false statement, || &&返回最后一个true语句或第一个false语句, || returns the first true statement or last false statement. 返回第一个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

and && is evaluated before || &&||之前进行评估 so 所以

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

Its also worth mentioning that falsy values encompass more than undefined in JS, so your 2 checks are not quite the same. 还值得一提的是,假值在JS中包含的不仅仅是未定义的,所以你的2个检查并不完全相同。 A value of 0 for instance, will be considered falsy, even if it is "defined" 例如,值0将被视为假,即使它是“已定义”

See the official spec here for the detailed logical specification. 有关详细的逻辑规范,请参阅此处的官方规范。

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. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM