Javascript条件订单评估 [英] Javascript conditional order evaluation

查看:94
本文介绍了Javascript条件订单评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当表达式中的一个条件导致错误时,我可以指望Javascript立即失败吗?

Can I count on Javascript failing immediately when one condition in an expression results to false?

f = {'a':'b'};
if (f.a !== undefined || f.a === 'b') {
  // Is this OK to use, because the second condition will never be evaluated?
}


推荐答案

是的,这被称为短路评估

使用 AND 逻辑运算符,如果第一个计算结果为 false ,然后第二个永远不会被评估,因为条件已经足够了解。

With an AND logical operator, if the first evaluates to false, then the second is never evaluated, because the condition knows enough already to be met.

使用 OR 逻辑运算符,如果第一个是 false ,它将评估第二个。否则,如果第一个是 true ,它将不会评估第二个(不需要)。

With the OR logical operator, if the first one is false, it will evaluate the second one. Otherwise if the first is true it won't evaluate the second (no need to).

这也是为什么你看... ...

This is also why you see...

var a = function(b) {
   b = b || 7;
}

这篇关于Javascript条件订单评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆