Why is it throwing error in case 1 but not in case 2?
case 1:
export class AppComponent implements OnInit{
obj={
name:"XYZ",
age:"22",
height:"5"
}
ngOnInit() {
this.calling();
}
calling(){
if(true){
Object.keys(this.obj).forEach(function(key)
{
console.log(key,this.obj[key])
}
)
}
}
}
error: Cannot read property 'obj' of undefined
case 2:
export class AppComponent implements OnInit{
ngOnInit() {
this.calling();
}
calling() {
if(true){
let obj={
name:"XYZ",
age:"22",
height:"5"
}
Object.keys(obj).forEach(function(key)
{
console.log(key,obj[key])
}
)
}
}
}
No error is shown in console in this case.