bumbeishvili bumbeishvili -4 years ago 69
Javascript Question

why I am getting, cannot read property of undefined error?

This code



function Test(){

}
new Test()



['width', 'height'].forEach(key => {
console.log(key);
});





gives me error

enter image description here

but this code not



function Test(){

}
new Test()

var arr = ['width', 'height'];
arr.forEach(key => {
console.log(key);
});





why?

I am using chrome
Version 53.0.2785.116 (64-bit) - Macbook air 2014

Answer Source

As per snapshot, you are using chrome and Chrome supports .forEach from a long time. My guess is, issue is due to minification or missing semicolon(;)

Sample

function test(){
  this.name = "test";
}

var a = new test()['width', 'height'].forEach(key => {
  console.log(key);
});

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download