Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property '0' of undefined

Tags:

javascript

When I call renderFrame, trying to access newLine[i] throws an error of

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]);
    }
},
like image 278
fredley Avatar asked Dec 03 '22 23:12

fredley


1 Answers

You forgot

return result;

at the end of generateLine()

So newLine is now undefined. Hence Cannot read property '0' of undefined

like image 154
Halcyon Avatar answered Dec 15 '22 21:12

Halcyon