JavaScript TypeError – 无法访问“Y”的属性“X”
如果对未定义或空值执行属性访问,则会发生此 JavaScript 异常无法访问属性。
信息:
TypeError: Unable to get property {x} of undefined or
null reference (Edge)
TypeError: can't access property {x} of {y} (Firefox)
TypeError: {y} is undefined, can't access property {x}
of it (Firefox)
TypeError: {y} is null, can't access property {x} of it
(Firefox)
例子:
TypeError: x is undefined, can't access property "prop"
of it
TypeError: x is null, can't access property "prop" of it
TypeError: can't access property "prop" of undefined
TypeError: can't access property "prop" of null
错误类型:
TypeError
错误原因:对代码中的任何未定义或空值执行了属性访问。
示例 1:在本示例中,GFG 未定义,因此发生错误。
HTML
HTML
输出(在控制台中):
TypeError: Unable to get property 'substring' of undefined or null reference
例2:在本例中,GFG为空,所以发生了错误。
HTML
输出(在控制台中):
TypeError: Unable to get property 'substring' of undefined or null reference