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

另見