if(typeof hello! = 'undefined'){
}
这太难打了,我想使用下划线
if(typeof hello! = 'undefined'){
}
这太难打了,我想使用下划线
_.isUndefined(hello) => Uncaught ReferenceError: hello is not defined(…)
(在Chrome 52中测试) - nickel715var a; _.isUndefined(a);
尝试访问未声明的变量将始终引发ReferenceError,除非通过 typeof a ==="undefined"
进行检查。然而,这个问题是关于如何通过Underscore.js检查未定义的。 - Platinum Azure
hello === undefined
应该能够正常运作.. 但是在许多情况下,我发现 - 并设计代码以使其成立 - 简单的“false-y”条件正确:if (!hello) ..
这里使用typeof
的唯一原因是 1)全局变量可能不存在(呃,为避免 ReferenceErrors 更喜欢window.prop
!)或 2)对“未定义”被“重新定义”过度谨慎。 - user166390