简体   繁体   English

Javascript中if语句中的评估顺序

[英]Order of evaluation in if statement in Javascript

In order to protect my code from accessing undeclared variables I use 为了保护我的代码不被访问我使用的未声明的变量

if (typeof myVar != 'undefined')

This works fine but I'd like to stick another if statement in it. 这工作正常,但我想在其中粘贴另一个if语句。 Something like converting this: 像转换这个:

if (typeof myVar != 'undefined'){
    if (myVar == "test"){}
}

to this: 对此:

if (typeof myVar != 'undefined' && myVar == "test")

Considering myVar may be undefined, is this last code secure in every case of usage and every browser ? 考虑到myVar可能是未定义的,这个最后的代码在每种使用情况和每个浏览器中都是安全的吗?

Is it possible that various statements inside an if () are not evaluated in the order they're written? 是否有可能if ()内的各种语句不按它们编写的顺序进行评估?

Can I assume myVar == "test" will never be executed if myVar is undefined? 如果myVar未定义,我可以假设myVar == "test" 永远不会被执行吗?

Can I assume myVar == "test" will never be executed if myVar is undefined? 如果myVar未定义,我可以假设myVar == "test"永远不会被执行吗?

Yes. 是。 The expression you are testing is a logical AND expression and the order of evaluation of the two operands is specified: 您正在测试的表达式是一个逻辑AND表达式,并指定了两个操作数的求值顺序:

The production LogicalANDExpression : LogicalANDExpression && BitwiseORExpression is evaluated as follows (emphasis added): 生产LogicalANDExpression : LogicalANDExpression && BitwiseORExpression的评估如下(强调添加):

  • Let lref be the result of evaluating LogicalANDExpression . lref成为评估LogicalANDExpression的结果。
  • Let lval be GetValue( lref ). lval为GetValue( lref )。
  • If ToBoolean( lval ) is false, return lval . 如果ToBoolean( lval )为false,则返回lval
  • Let rref be the result of evaluating BitwiseORExpression . rref是评估BitwiseORExpression的结果。
  • Return GetValue( rref ). 返回GetValue( rref )。

That basically says evaluate the first operand, if the result of that evaluation is false , the entire expression is false , and the second operand is never evaluated. 这基本上表示评估第一个操作数,如果该评估的结果为false ,则整个表达式为false ,并且永远不会计算第二个操作数。

是,如果您使用&&运算符,那么如果第一个条件为假,则第二个将永远不会测试。

Yes, it is OK. 是的,没关系。

In javascript, the precedence is as below: 在javascript中,优先级如下:

typeof > !=/== > && typeof > !=/== > &&

For a && b , b will never be executed when a is false. 对于a && b ,当a为假时, b将永远不会被执行。

if (typeof myVar != 'undefined' && myVar == "test")

表达式从左到右进行评估,如果myVar undefined则会短路 (myVar不会与“test”进行比较)

Whenever you use the && operator, the conditions that you pass in will be evaluated in the order that they were passed in. 每当使用&&运算符时,传入的条件将按照传入的顺序进行计算。

In your case: 在你的情况下:

if (typef myVar != 'undefined' && myVar == 'test')

When the program enters this if statement, it first checks if the variable type is undefined, if it returns true, it continues to the second argument and checks if myVar has a value of 'test'. 当程序输入if语句时,它首先检查变量类型是否未定义,如果返回true,则继续第二个参数并检查myVar是否具有'test'值。 Even if myVar == 'test', the if statement will evaluate the value of false && true, which returns true. 即使myVar =='test',if语句也会计算false && true的值,返回true。 Only if both conditions return true, the code block will get executed, because the expression will be evaluated as true (true && true). 只有当两个条件都返回true时,才会执行代码块,因为表达式将被计算为true(true && true)。

If the first condition 如果是第一个条件

typeof myVar != 'undefined'

returns false, the second condition will never get evaluated. 返回false,第二个条件永远不会被评估。

Very Important! 很重要! Use 使用

if (myVar !== undefined)

This means that is no way that myVar will pass if it had any assignment before. 这意味着如果以前有任何赋值,myVar将无法通过。

AND DONT USE 'undefined' THIS IS A SIMPLE STRING! 并且不要使用'undefined'这是一个简单的字符串!

Good luck! 祝好运!

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

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