Understanding the Distinction: variable === undefined vs. typeof variable === "undefined"
In the jQuery Core Style Guidelines, two methods are proposed to verify if a variable is defined:
Why this difference?
The explanation lies in the behavior of these operators when dealing with undeclared variables.对于未申报的变量,FOO TypeOf Type将返回“未定义”作为字符串。但是,身份检查foo ===未定义会引起错误“未定义foo”。
与局部变量对比。由于它们被明确声明在某个地方声明,因此尝试身份检查变量===未定义不会触发错误。因此,jQuery使用typeof Operator进行全局变量(可能被声明或可能不会声明),以避免潜在的错误。对于确保声明的本地变量和属性,由于其简单性,因此优选身份检查。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3