Israelg99 Israelg99 - 1 year ago 77
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download