0

I am learning angular, following one tutorial and my code shows error while if i copy the code of the instructor, it does work even though it seems to me that the only difference in our codes is this one function wrote differently.

I spent like 2 hours looking at this code, compared his and mine versions but still cant figure out the problem.

Mine code:

calculateOrders() {
  this.customersOrderTotal = 0;
  this.filteredCustomers.forEach(function(cust: ICustomer){
    this.customersOrderTotal += cust.orderTotal;

  });
}

His code:

calculateOrders() {
  this.customersOrderTotal = 0;
  this.filteredCustomers.forEach((cust: ICustomer) => {
    this.customersOrderTotal += cust.orderTotal;
  });
}
VLAZ
  • 18,437
  • 8
  • 35
  • 54
tkmiro
  • 1
  • 2
    Arrow functions maintain `this`, a normal function will have it's own `this` – Keith Apr 05 '19 at 16:18
  • Also relevant: https://stackoverflow.com/questions/22939130/when-should-i-use-arrow-functions-in-ecmascript-6 – VLAZ Apr 05 '19 at 16:20

0 Answers0