Israelg99 Israelg99 - 7 months ago 36
AngularJS Question

private members are accessible in angular 2 decorators

Consider this code:

export class Hero {
constructor(private id: number, private name: string) {}

selector: 'my-app',
template: '<h1>{{title}}</h1><h2>{{}} details!</h2>'
export class AppComponent {
private title = "Tour of Heroes";
private hero: Hero = new Hero(1, "Windstorm");

's template I wrote
, however, this field is private according to the
class and should not be accessible.
How this code compiles and works? am I missing something?


In JavaScript there is no such thing as private variables. Keywords like private are just used by the TypeScript transpiler to enforce constraints before transpilation. Once the code is transpiled into JavasScript, the name property is a visible member of the Hero class.