2013-10-10 2 views
11

Es gab einen Vorschlag von Code wie dieseWie greifen Sie auf Eigenschaften der Basisklasse in Typescript zu?

class A { 
    // Setting this to private will cause class B to have a compile error 
    public x: string = 'a'; 
} 

class B extends A { 
    constructor(){super();} 
    method():string { 
     return super.x; 
    } 
} 

var b:B = new B(); 
alert(b.method()); 

und es wurde sogar 9 Stimmen. Aber wenn Sie es auf dem offiziellen TS Spielplatz http://www.typescriptlang.org/Playground/ einfügen gibt es Ihnen und Fehler.

Wie Zugriff auf die X-Eigenschaft von A von B?

Antwort

27

Verwendung this statt super:

class A { 
    // Setting this to private will cause class B to have a compile error 
    public x: string = 'a'; 
} 

class B extends A { 
    // constructor(){super();} 
    method():string { 
     return this.x; 
    } 
} 

var b:B = new B(); 
alert(b.method()); 
+2

-Meister! Sorry, habe nicht genug Reputation zu +1 –

+4

@AlexVaghin Sie können/sollten als Antwort markieren – basarat