7
類
之外,我不能transpile這小小的一段代碼:「超級」的功能或
class FooBar extends SomeParent {
constructor() {
super();
}
start() {
var foo = function() {
super.start(); // <-- Error: 'super' outside of function or class
};
}
}
拋出的錯誤是'super' outside of function or class
。
但是,相同的代碼在Babel REPL中轉換正常。
我使用的這個命令的自定義Node.js的程序transpiling:
babel.transformFileSync(filename, { presets: [ 'es2015' ] });
安裝信息:
$ npm ls --global --depth 0
/usr/lib
├── [email protected]13
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
$ node -v
v0.10.40
我在做什麼錯?當使用Babel 5進行運輸時,我沒有任何問題...
可能是因爲REPL使用了babel 5而您使用的是babel 6? – Jivings
使用箭頭函數而不是常規函數,這將確保'super'來自父範圍。我不確定究竟哪個實現是正確的,但是我會猜測* Babel 6,因爲在類之外訪問'super' – CodingIntrigue
請參閱[「應該在其標題中包含」標籤「? 「](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles),其中的共識是」不,他們不應該「! –