javascripts typeof表达式是否检查为null?
var test = {};
console.log(typeof test['test']);//"undefined"
var test = null;
console.log(typeof test['test']);//TypeError: test is null
Run Code Online (Sandbox Code Playgroud)
显然,但为什么错误,如果typeof null是一个对象?
编辑:
我知道如何避免类型错误,并且null没有属性,但我想知道是否有行为的解释typeof.
var test = { test: null };
console.log(typeof test['test']);// will be object
Run Code Online (Sandbox Code Playgroud)
您的代码抛出异常,因为您正在读取null的属性,如下所示:
null['test']
Run Code Online (Sandbox Code Playgroud)