TypeError:无法读取未定义的属性“0”

时间:2013-05-21 18:59:23

标签: javascript

当我致电renderFrame时,尝试访问newLine[i]会引发错误

Uncaught TypeError: Cannot read property '0' of undefined

这是为什么?我已经包含了相关的代码。

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]);
    }
},

1 个答案:

答案 0 :(得分:17)

你忘记了

return result;

generateLine()

的末尾

所以newLine现在未定义。因此Cannot read property '0' of undefined