4

当我打电话时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]);
    }
},
4

1 回答 1

17

你忘了

return result;

在......的最后generateLine()

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

于 2013-05-21T19:00:15.540 回答