Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private variable naming convention in official Angular 2 http tutorial

Tags:

angular

I am trying to understand how private variable is named in the official Angular 2 http tutorial

Under the section linked above is a file called app/toh/hero.service.ts, which (mainly) is this:

@Injectable()
export class HeroService {
  constructor (private http: Http) {}

  private _heroesUrl = 'app/heroes';

  getHeroes () {
    return this.http.get(this._heroesUrl)
                    .map(res => <Hero[]> res.json().data)
                    .catch(this.handleError);
  }
  private handleError (error: Response) {
    // in a real world app, we may send the server to some remote logging infrastructure
    // instead of just logging it to the console
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
  }
}

There is a private variable _heroesUrl. Ok, so there exists a convention to start private variables and methods with underscore.

But then why is it not used an underscore also for private http and private handleError? Is it just a "typo" or is it a reason for this?

like image 814
EricC Avatar asked Feb 07 '16 16:02

EricC


1 Answers

This is just a typo. For TS this is not enforced, it's just a convention. Within the Angular2 codebase this is used consistently to allow transpilation to Dart where the _ is not only a convention but a replacement for the private keyword (which doesn't exist in Dart)

like image 117
Günter Zöchbauer Avatar answered Oct 31 '22 21:10

Günter Zöchbauer