I'm new to javascript, so when I was trying to iterate an array which is an attribute of an object, using a method of the same object, It is showing error. This is the code:
let obje = {
data : [1,2,3,4],
print : ()=> {
this.data.forEach(element => {
console.log(element);
});
}
}
obje.print()
This is the error i get:
Uncaught TypeError: Cannot read property 'forEach' of undefined
at Object.print (script.js:13)
at script.js:19
But when i do this same thing without arrow function, it works
let obje = {
data : [1,2,3,4],
print : function() {
this.data.forEach(element => {
console.log(element);
});
}
}
obje.print()
Output:-
1
2
3
4