gpt4 book ai didi

javascript - JavaScript 的 typeof 函数是否检查 null

转载 作者:行者123 更新时间:2023-11-29 16:15:08 25 4
gpt4 key购买 nike

javascripts typeof 表达式是否检查 null?

var test = {};
console.log(typeof test['test']);//"undefined"

var test = null;
console.log(typeof test['test']);//TypeError: test is null

很明显,但是如果 typeof null 是一个对象,为什么会出错?

编辑:
我知道如何避免类型错误,并且 null 没有属性,但我想知道是否有对 typeof 行为的解释。

最佳答案

var test = { test: null };
console.log(typeof test['test']);// will be object

您的代码抛出异常,因为您正在像这样读取 null 的属性:

null['test']

关于javascript - JavaScript 的 typeof 函数是否检查 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17701173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com