[英]How does typeof circumvent the ReferenceError when supplied an undeclared variable identifier?
foo; // ReferenceError: foo is not defined
typeof(foo); // undefined
How does typeof
circumvent the ReferenceError
when supplied an undeclared variable identifier? 当提供未声明的变量标识符时, typeof
如何绕过ReferenceError
? Is this just JavaScript interpreter "magic" or can it be explained in terms of user-land concepts? 这只是JavaScript解释器“神奇”还是可以用用户土地概念来解释?
No, this cannot be explained in user-land concepts - it's "magic" if you want. 不,这在用户土地概念中无法解释 - 如果你愿意,这是“神奇的”。
EcmaScript uses the Reference specification type to explain cases like this. EcmaScript使用Reference规范类型来解释这样的情况。 These references are used to describe the semantics of assignments, method calls , eval
and many more. 这些引用用于描述赋值, 方法调用 , eval
等语义。 Typically, the GetValue algorithm is called on them to dereference them (eg in the evaluation of your expression statement), and this does throw the ReferenceError
when the reference is not resolvable. 通常,在它们上调用GetValue算法以取消引用它们(例如,在表达式语句的求值中),并且当引用不可解析时,这会抛出ReferenceError
。
The typeof
operator in contrast does not just do GetValue, but has a special case to handle these undeclared-variable references: 相比之下, typeof
运算符不只是执行GetValue,而且还有一个特殊情况来处理这些未声明的变量引用:
If
Type(val)
is Reference , then 如果Type(val)
是Reference ,那么a. 一个。 If
IsUnresolvableReference(val)
istrue
, return"undefined"
. 如果IsUnresolvableReference(val)
为true
,则返回"undefined"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.