使用 typeof vs === 检查未声明的变量会产生不同的结果
Using typeof vs === to check undeclared variable produces different result
如果我有一个未声明的变量并使用 typeof
,它会告诉我它是 undefined
。
但是,如果我随后使用 if (qweasdasd === undefined)
检查它,它会抛出异常。
我不明白这种行为,因为如果第一个告诉 undefined
,那么第二个检查的结果应该是 if (undefined === undefined)
,为什么会抛出 ReferenceError 异常?
尝试读取未声明变量的值(在将该值与 undefined
的值进行比较之前必须这样做)会引发 ReferenceError。
将 typeof
运算符应用于未声明的变量不会。
typeof
看起来像一个函数调用,但它不是——它是一个运算符。允许操作员违反规则。 typeof(qweasdasd)
不假设 qweasdasd
存在;它是否存在以及它是什么 typeof
的存在是为了发现。但是,当您测试 qweasdasd === undefined
时,您使用的是 qweasdasd
作为值,而当您使用尚未为其赋值的变量时,JS 会报错。
您有一个未定义的变量,但不是未定义的对象值。
console.log(variable) // undefined
console.log(typeof variable) // "undefined"
console.log(variable === undefined) // Reference error
variable = undefined ;
console.log(variable === undefined) // true
尝试访问 Javascript 中未定义的变量会得到 ReferenceError
。 typeof
正在工作,因为它没有访问变量值。正在检查其类型。
typeof 不需要定义变量。它的参数应该是一个表达式,表示要返回其类型的对象或基元。
typeof
是一个运算符,它 returns 你一个字符串,指示未计算的操作数的类型。未计算的操作数是否未定义并不重要。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
>>> if (a !== 'undefined');
Exception: ReferenceError: Can't find variable: a
>>> if (typeof a !== undefined);
undefined
>>> if (typeof a !== void 0);
undefined
我想我对此有一个非常简单的解释——因为规格是这样说的:
如果未定义变量,typeof
operator 不应抛出 ReferenceError 异常
- If Type(val) is Reference, then
a. If IsUnresolvableReference(val) is true, return "undefined"
.
如果操作数引用未定义的变量,===
operator 应该抛出 ReferenceError 异常
- Let lval be GetValue(lref).
[And inside the definition of GetValue we have]
- Let base be the result of calling GetBase(V).
- If IsUnresolvableReference(V), throw a ReferenceError exception.
如果我有一个未声明的变量并使用 typeof
,它会告诉我它是 undefined
。
但是,如果我随后使用 if (qweasdasd === undefined)
检查它,它会抛出异常。
我不明白这种行为,因为如果第一个告诉 undefined
,那么第二个检查的结果应该是 if (undefined === undefined)
,为什么会抛出 ReferenceError 异常?
尝试读取未声明变量的值(在将该值与 undefined
的值进行比较之前必须这样做)会引发 ReferenceError。
将 typeof
运算符应用于未声明的变量不会。
typeof
看起来像一个函数调用,但它不是——它是一个运算符。允许操作员违反规则。 typeof(qweasdasd)
不假设 qweasdasd
存在;它是否存在以及它是什么 typeof
的存在是为了发现。但是,当您测试 qweasdasd === undefined
时,您使用的是 qweasdasd
作为值,而当您使用尚未为其赋值的变量时,JS 会报错。
您有一个未定义的变量,但不是未定义的对象值。
console.log(variable) // undefined
console.log(typeof variable) // "undefined"
console.log(variable === undefined) // Reference error
variable = undefined ;
console.log(variable === undefined) // true
尝试访问 Javascript 中未定义的变量会得到 ReferenceError
。 typeof
正在工作,因为它没有访问变量值。正在检查其类型。
typeof 不需要定义变量。它的参数应该是一个表达式,表示要返回其类型的对象或基元。
typeof
是一个运算符,它 returns 你一个字符串,指示未计算的操作数的类型。未计算的操作数是否未定义并不重要。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
>>> if (a !== 'undefined');
Exception: ReferenceError: Can't find variable: a
>>> if (typeof a !== undefined);
undefined
>>> if (typeof a !== void 0);
undefined
我想我对此有一个非常简单的解释——因为规格是这样说的:
-
如果未定义变量,
typeof
operator 不应抛出 ReferenceError 异常
- If Type(val) is Reference, then
a. If IsUnresolvableReference(val) is true, return"undefined"
.
-
如果操作数引用未定义的变量,
===
operator 应该抛出 ReferenceError 异常
- Let lval be GetValue(lref).
[And inside the definition of GetValue we have]
- Let base be the result of calling GetBase(V).
- If IsUnresolvableReference(V), throw a ReferenceError exception.