javascript 类型错误:无法读取未定义的属性“0”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16677499/
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-10-27 05:36:05 来源:igfitidea点击:
TypeError: Cannot read property '0' of undefined
提问by fredley
When I call renderFrame
, trying to access newLine[i]
throws an error of
当我打电话时renderFrame
,尝试访问newLine[i]
会引发错误
Uncaught TypeError: Cannot read property '0' of undefined
Why is this? I've included the relevant code.
为什么是这样?我已经包含了相关的代码。
rows : ["-","-","-","-","-","-","-","-","-","-",],
generateLine : function(){
var result = new Array(10);
for(var i = 0; i < result.length; i++){
result[i] = " ";
}
},
renderFrame : function(){
var newLine = this.generateLine();
for(var i = 0; i < this.rows.length; i++){
console.log(newLine[i]);
}
},
回答by Halcyon
You forgot
你忘了
return result;
at the end of generateLine()
在......的最后 generateLine()
So newLine
is now undefined. Hence Cannot read property '0' of undefined
所以newLine
现在未定义。因此Cannot read property '0' of undefined