TypeError: null/undefined has no properties

当您尝试访问 nullundefined 的属性时,会发生 JavaScript 异常 "null(或 undefined)没有属性"。它们没有任何属性。

消息

TypeError: Cannot read properties of undefined (reading 'x') (V8-based)
TypeError: Cannot destructure 'x' as it is undefined. (V8-based)
TypeError: Cannot destructure property 'x' of 'y' as it is undefined. (V8-based)
TypeError: null has no properties (Firefox)
TypeError: undefined has no properties (Firefox)
TypeError: undefined is not an object (evaluating 'undefined.x') (Safari)
TypeError: Right side of assignment cannot be destructured (Safari)

错误类型

TypeError.

哪里出错了?

无论是 null 还是 undefined 都没有您可以访问的属性。因此,您不能在它们上使用属性访问器,也不能对它们进行解构

示例

null 和 undefined 没有属性

js
null.foo;
// TypeError: null has no properties

undefined.bar;
// TypeError: undefined has no properties

另见