Here is a function example:
(function iterate(index) {
var obj = { 0:{name:"John Doe"}, 1:{age: 30}, 2:{years: 5} };
setTimeout(function() {
//code ...
}, (obj[0][years]*1000) );
)(0);
Encountering an error in the second last line
myscript.js:7 Uncaught TypeError: Cannot read property '0' of undefined
This issue points to a scoping problem, as attempts to access the 0th key using obj[0] or obj.0 have been unsuccessful.
Based on my understanding, the obj
variable is defined in the same scope as the setTimeout()
function call. So, what is causing this error and how can it be resolved?