I have either a typescript or javascript syntax issue. Can someone tell me what _ => this.log... means?
I am used to seeing a name the parameter being passed into the arrow function there.
Does it simply mean 'no parameter'?
Ref: https://angular.io/tutorial/toh-pt6#add-heroserviceupdatehero
/** PUT: update the hero on the server */
updateHero (hero: Hero): Observable<any> {
return this.http.put(this.heroesUrl, hero, httpOptions).pipe(
tap(_ => this.log(`updated hero id=${hero.id}`)),
catchError(this.handleError<any>('updateHero'))
);
}
With the minor difference that _ would be accessible as an argument within the arrow function (although it will likely have the value undefined ), whereas the second snippet won't have any accessible arguments.
The Tour of Heroes application that you build helps a staffing agency manage its stable of heroes. The application has many of the features that you'd expect to find in any data-driven application. The finished application: Gets a list of heroes. Displays the heroes in a list.
() => {console.log('Hello World')}
_ => {console.log('Hello World')}
Both of the above are just the same if your function doesn't need a parameter.
The underscore _
is just a throwaway variable, meaning it can be any variable name since it will never be used. It's just that they usually use the underscore to say that the function doesn't need a parameter.
I write my functions with no parameters using ()=>
, but I've seen a lot of versions using the underscore so it's good to understand both.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With