Javascript “无法读取 null 的属性‘xxx’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2711843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:35:10  来源:igfitidea点击:

"Cannot read property 'xxx' of null"

javascriptnulltypeerror

提问by Matrym

I've got a document fragment with children that I want to loop over (when possible). This is causing an error "Cannot read property 'xxx' of null".

我有一个带孩子的文档片段,我想循环播放(如果可能)。这会导致错误“无法读取 null 的属性 'xxx'”。

How do I test if this will be the case?

我如何测试是否会出现这种情况?

采纳答案by Daniel Vassallo

You may want to do the following:

您可能想要执行以下操作:

if ((documentFragment !== null) && documentFragment.hasOwnProperty('xxx')) {
    // handle property xxx of documentFragment as required
}