简体   繁体   English

当提供未声明的变量标识符时,typeof如何绕过ReferenceError?

[英]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,而且还有一个特殊情况来处理这些未声明的变量引用:

  1. If Type(val) is Reference , then 如果Type(val)Reference ,那么

    a. 一个。 If IsUnresolvableReference(val) is true , return "undefined" . 如果IsUnresolvableReference(val)true ,则返回"undefined"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这种情况下,javascript 将 undefined 分配给未声明的变量,并且在访问变量时抛出 referenceError - In which cases the javascript assigns undefined to undeclared variable and in which throw referenceError when access a variable 为什么typeof有时会抛出ReferenceError? - Why does typeof only sometimes throw ReferenceError? ReferenceError:分配给未声明的变量petsPrice_cal - ReferenceError: assignment to undeclared variable petsPrice_cal 仅当使用 ECMAScript 模块时,对未声明变量的赋值才会在 Node 中抛出 ReferenceError - Assignment to an undeclared variable throws ReferenceError in Node only when using ECMAScript modules Google闭包编译器抱怨typeof调用中的未声明变量 - Google closure-compiler complaining about undeclared variable in typeof call 使用typeof vs ===检查未声明的变量会产生不同的结果 - Using typeof vs === to check undeclared variable produces different result Snowpack & Peerjs: Uncaught ReferenceError: assignment to undeclared variable parcelRequire - Snowpack & Peerjs: Uncaught ReferenceError: assignment to undeclared variable parcelRequire AJAX调用失败,并将ReferenceError分配给未声明的变量PostRPC - AJAX call is failing with ReferenceError assignment to undeclared variable PostRPC jQuery为什么以及如何使用未声明的变量作为ID - why and how does jQuery use an undeclared variable as an ID Javascript typeof 抛出引用错误 - Javascript typeof throws referenceerror
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM