2013-10-10 13 views
11

C'era un suggerimento di utilizzare il codice come questoCome si accede alle proprietà della classe base in Typescript?

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()); 

ed è anche ottenuto 9 voti. Ma quando lo incolli nel parco giochi ufficiale di TS http://www.typescriptlang.org/Playground/ ti dà ed errore.

Come accedere alla proprietà x di A da B?

risposta

27

uso this piuttosto che 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

Champion! Spiacenti, non ho abbastanza reputazione per +1 –

+4

@AlexVaghin puoi/dovrebbe contrassegnare come risposta – basarat