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?
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)
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