I'm new to arrow functions and I don't understand why I can use this code:
const adder = {
sum: 0,
add(numbers) {
numbers.forEach(n => {
this.sum += n;
});
}
};
adder.add([1,2,3]);
// adder.sum === 6
... and it works just fine, but in the following case the this
is not bound properly:
const adder = {
sum: 0,
add: (numbers) => {
numbers.forEach(n => {
this.sum += n;
});
}
};
adder.add([1,2,3]);
// Cannot read property sum