2013-10-10 17 views
11

Było sugestia użyciu kodu jak tenJak uzyskać dostęp do właściwości klasy bazowej w Maszynopisie?

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

i nawet dostał 9 głosów. Ale po wklejeniu go na oficjalnym placu zabaw TS http://www.typescriptlang.org/Playground/ daje ci i błąd.

Jak uzyskać dostęp do właściwości x A z B?

Odpowiedz

27

użycie this zamiast 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! Przepraszamy, nie masz wystarczającej reputacji +1 –

+4

@AlexVaghin możesz/powinien oznaczać jako odpowiedź – basarat

Powiązane problemy