2
我創建了一個foo.ts
這樣的:如何在Nodejs REPL中使用Typescript類?
class Foo{
public echo(){
console.log("foo");
}
}
,並將其輸出這樣的javascript代碼:
var Foo = (function() {
function Foo() {
}
Foo.prototype.echo = function() {
console.log("foo");
};
return Foo;
})();
我想打電話給echo
功能的NodeJS REPL,但它結束了這樣的錯誤:
$ node
> require('./foo.js');
{}
> f = new Foo
ReferenceError: Foo is not defined
at repl:1:10
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.EventEmitter.emit (events.js:98:17)
at emitKey (readline.js:1095:12)
如何實例化類並調用函數echo
?
我不知道如何typescript工作,但很明顯,你是不是從Foo.js任何出口(並且你沒有分配要求紅色模塊到任何東西)。也許首先讓你自己熟悉Node的模塊系統。 –