dro*_*gon 0 javascript typeof referenceerror
考虑以下代码:
//the var Test has NOT been declared yet
console.log((typeof Test)); // "undefined"
console.log(Test); //"Uncaught ReferenceError: Test is not defined"
Run Code Online (Sandbox Code Playgroud)
为什么第二个console.log语句抛出一个ReferenceError,第一个显示未定义.
因为测试未定义.
首先,console.log你要求系统告诉你变量的类型.因此,它查看当前作用域链以查找对该变量的引用,以便它可以推断其类型.
当它找不到变量时,它会接收undefined原语.正如我相信你已经猜到的那样undefined.
第二次要求它打印出未定义变量的值.由于未定义变量(当前作用域链中没有对它的引用),这是您尝试访问不存在的数据的错误,而不仅仅是推断它的类型.
| 归档时间: |
|
| 查看次数: |
395 次 |
| 最近记录: |