Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to understand lambdas expression in angular2?

Tags:

angular

Angular 2 code

 heroes => this.heroes = heroes;

please tell me what this line exactly mean to?

like image 528
Mr_Perfect Avatar asked Apr 26 '26 06:04

Mr_Perfect


1 Answers

heroes => this.heroes = heroes;

Is a function that takes a parameter heroes and the body of the function is

this.heroes = heroes

which assigns the parameter to this.heroes.

The arrow function has an implicit return which means that the return value is the result of the body.

this.heroes = heroes

returns heroes

The main difference is that => instead of

function(heroes) { this.heroes = heroes; }

ensures that this. keeps pointing to the current class instance similar to

(function(heroes) { this.heroes = heroes; }).bind(this)
like image 97
Günter Zöchbauer Avatar answered Apr 28 '26 05:04

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!