带比较运算符的布尔表达式的typeof [英] typeof of boolean expression with comparison operator

查看:94
本文介绍了带比较运算符的布尔表达式的typeof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (typeof foo !== 'undefined') {
    // Now we know that foo is defined, we are good to go.
}

typeof 评估根据变量 foo 是否为 true false 定义与否。

The typeof evaluates to true or false based on whether the variable foo is defined or not.

但是,如果 foo!=='undefined'评估为 true ,然后 typeof true 应评估为'布尔'
为什么它评估为 true false

But, say if foo !== 'undefined' evaluates to true, then typeof of true should evaluate to 'boolean'. Why does it evaluate to true or false?

推荐答案

因为 typeof 和不等式运算符的优先规则定义该表达式被解析为

Because precedence rules for the typeof and inquality operators define that that expression is parsed as

(typeof foo) !== 'undefined'

有关详细信息,请参阅 MDN页面关于运营商优先级 typeof 优先级为16; inquality优先级10. typeof 的优先级越高意味着它绑定到它的操作数更紧密。

For more information, see the MDN page on operator precedence. typeof is precedence 16; inquality is precedence 10. The higher precedence of typeof means that it "binds" more tightly to its operand.

顺便说一下,为什么你的代码中有未定义的变量?

By the way, why do you have undefined variables in your code?

这篇关于带比较运算符的布尔表达式的typeof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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