node.js coffeescript – 需要模块的问题
我有另一个问题node.js,这次我不能让我的JavaScript代码来识别一个咖啡模块的类有function。
在我的主文件,main.js我有以下代码:
require('./node_modules/coffee-script/lib/coffee-script/coffee-script'); var Utils = require('./test'); console.log(typeof Utils); console.log(Utils); console.log(typeof Utils.myFunction);
在我的模块test.coffe中,我有以下代码:
class MyModule myFunction : () -> console.log("debugging hello world!") module.exports = MyModule
这是我运行node main.js
时的输出:
function [Function: MyModule] undefined
我的问题是,为什么我的主文件加载正确的模块,但为什么它无法访问该function? 我做错了什么,不pipe是用coffeescript语法,还是我要求我的模块? 让我知道我是否应该澄清我的问题。
谢谢,
维尼特
myFunction
是一个实例方法,所以不能直接从class
访问。
如果你想要它作为一个类 (或静态 )方法,用@
作为前缀名引用类:
class MyModule @myFunction : () -> # ...
如果所有方法的意图都是静态的,你也可以导出一个Object
:
module.exports = myFunction: () -> # ...
否则,你需要创build一个实例,或者在main
:
var utils = new Utils(); console.log(typeof utils.myFunction);
或者,作为出口对象:
module.exports = new Utils