-1

I work on code snippet described on https://angular.io/tutorial/toh-pt5

export class DashboardComponent implements OnInit {
  heroes: Hero[] = [];
  constructor(private heroService: HeroService) { }
  ngOnInit(): void {
    this.heroService.getHeroes()
      .then(heroes => this.heroes = heroes.slice(1, 5));
  }
}

And my question about this code part:

.then(heroes => this.heroes = heroes.slice(1, 5));

is it possible to create a method in DashboardComponent and pass it as argument instead of function? Is the functional approach here anything better than a method inside DashboardComponent class?

Pavlo Morozov
  • 2,510
  • 4
  • 25
  • 58

1 Answers1

1

There are several reasons why using arrow functions. Most of them are just a matter of style & readability.

Beside that, there is one important difference: if you would be passing components method to it then you would need to bind contrxt for this to that method. With arrow function thats not needed as they dont create new context.

dee zg
  • 12,355
  • 8
  • 36
  • 64