gpt4 book ai didi

javascript - JavaScript 错误 "Cannot read property ' foo' of null"和 "null is not an object"之间有什么区别

转载 作者:行者123 更新时间:2023-11-29 18:05:33 38 4
gpt4 key购买 nike

我们正在记录 javascript 错误,一条经常出现的错误消息是 null is not an object,例如 TypeError: null is not an object (evaluating 'foo[栏]')。在搜索此错误并尝试复制错误时,我不断收到 Cannot read property 'foo' of null 错误。我想不出一种方法来执行导致 null is not an object 错误的操作。

这两个错误之间有什么区别?什么时候会调用一个错误而不是另一个错误?

最佳答案

一个例子is here

当您将 null 值分配给一个对象并尝试访问它的任何属性时,您将得到 null is not an object。

所以,基本上,如果您尝试在具有 null 值的对象上调用任何函数,将会出现此错误:

  • null is not an object in Safari

  • Cannot read property of null in Chrome

  • TypeError: obj[1] is null in Firefox

关于javascript - JavaScript 错误 "Cannot read property ' foo' of null"和 "null is not an object"之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549230/

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