2017-09-15 84 views
0

我嘗試調用父類的方法,但我得到的錯誤:錯誤:調用super之外類的構造函數的

Error: call super outside of class constructor

我的例子:

class xo{ 
    cool(x){ 
     console.log(`parent init${x}`) 
    } 
} 
class boo extends xo{ 
    cool(val){ 
     super(val); 
     console.log(`child init${x}`) 
    } 
} 

x = new boo; 

回答

3

使用super.cool(val)轉而呼籲超級cool方法類。 super()調用超類的構造函數。

4

你打電話不是父方法,但父母構造這不是構造之外有效的呼叫。您需要使用super.cool(val);而不是super(val);

class xo{ 

    cool(x) { 
     console.log(`parent init${x}`) 
    } 

} 

class boo extends xo { 

    cool(val) { 
     super.cool(val); 
     console.log(`child init${x}`) 
    } 

} 

x = new boo(); 
+0

@zloctb。如果你的問題的答案可以幫助你,你可以接受它。 –

相關問題