Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Underscore for in front of the arrow function (_=>) in the hero.service.ts Angular v5 Tour of Heroes Tutorial?

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'))
  );
}
like image 924
OpTech Marketing Avatar asked Dec 22 '17 04:12

OpTech Marketing


People also ask

What does _ do in angular?

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.

What is Tour of Heroes?

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.


1 Answers

() => {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.

like image 90
dev mamba Avatar answered Sep 28 '22 07:09

dev mamba