最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the Underscore for in front of the arrow function (_=>) in the hero.service.ts Angular v5 Tour of He

programmeradmin1浏览0评论

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:

    /** 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'))
  );
}

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'))
  );
}
Share Improve this question edited Dec 30, 2022 at 15:14 31piy 23.9k6 gold badges51 silver badges68 bronze badges asked Dec 22, 2017 at 4:04 OpTech MarketingOpTech Marketing 4372 gold badges7 silver badges19 bronze badges 1
  • Possible duplicate of Using _ (underscore) variable with arrow functions in ES6/Typescript – Carolus Commented Jul 25, 2019 at 14:33
Add a comment  | 

2 Answers 2

Reset to default 13

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

Its nothing but a notion to name a parameter which isn't going to be used in the function.

Instead, they would have written it like this:

tap(() => this.log(`updated hero id=${hero.id}`)),

If you want to read more, this post is a good start.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论