在没有初始化的情况下声明变量会使它成为非未定义的情况吗?
Is there a case when declaring a variable without initializing it would make it non-undefined?
如果我们声明一个没有初始值的变量,那么该值将是undefined
:
var foo;
foo;
// → undefined
但是,是否存在这样的情况(例如某些旧浏览器,或者可能是特殊环境)在声明变量时,该变量的值可能不同于 undefined
?
我只是在想为什么在某些情况下我们使用 void 0
而不是 undefined 来检查 undefined
?
// Why this...
if (foo === void 0) {
// ...
}
// and not this?
var U;
if (foo === U) {
...
}
您不能覆盖 window.undefined
。
过去在某些浏览器中这曾经是可能的,但现在不可能了。
如评论中所述,您可以创建一个 undefined
变量,如下所示:
(() => {
const undefined = 'baz';
console.log('baz' === undefined)
})()
如果我们声明一个没有初始值的变量,那么该值将是undefined
:
var foo;
foo;
// → undefined
但是,是否存在这样的情况(例如某些旧浏览器,或者可能是特殊环境)在声明变量时,该变量的值可能不同于 undefined
?
我只是在想为什么在某些情况下我们使用 void 0
而不是 undefined 来检查 undefined
?
// Why this...
if (foo === void 0) {
// ...
}
// and not this?
var U;
if (foo === U) {
...
}
您不能覆盖 window.undefined
。
过去在某些浏览器中这曾经是可能的,但现在不可能了。
如评论中所述,您可以创建一个 undefined
变量,如下所示:
(() => {
const undefined = 'baz';
console.log('baz' === undefined)
})()