【发布时间】:2020-02-18 06:30:31
【问题描述】:
我像这样创建了foo.ts:
class Foo{
public echo(){
console.log("foo");
}
}
它会输出这样的javascript代码:
var Foo = (function () {
function Foo() {
}
Foo.prototype.echo = function () {
console.log("foo");
};
return Foo;
})();
我想在 nodejs REPL 中调用 echo 函数,但它最终会出现这样的错误:
$ 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 的模块系统。
标签: javascript node.js typescript