[英]Type checking undefined in jQuery style guide
jQuery has a style guide here: http://contribute.jquery.org/style-guide/js/#type-checks jQuery在这里有一个样式指南: http : //contribute.jquery.org/style-guide/js/#type-checks
Type checking undefined
varies with scope: 类型检查
undefined
因范围而异:
undefined: 未定义:
Why is it necessary and/or preferred to vary this type check? 为什么有必要和/或首选改变这种类型检查?
typeof x
can not throw ReferenceError if there is no variable called x
. 如果没有名为
x
变量, typeof x
不能抛出ReferenceError。 If you try to typeof Boolaen === 'undefined'
(typo is intentional) it will return true
, if you try Boolaen === undefined
it will throw. 如果你尝试输入
typeof Boolaen === 'undefined'
(错字是故意的)它将返回true
,如果你尝试Boolaen === undefined
它会抛出。 You can check it in your browsers console. 您可以在浏览器控制台中查看它。
Further reference: ES 5.1, 11.4.3 - typeof operator 进一步参考: ES 5.1,11.4.3 - typeof operator
If a global variable hasn't been declared, doing variable === undefined
would throw an error ("variable is not defined"). 如果尚未声明全局变量,则执行
variable === undefined
会抛出错误(“ variable === undefined
变量”)。 With local variables you know that they're declared so you don't need to take that possibility into account. 使用局部变量,您知道它们已被声明,因此您不需要考虑这种可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.