node.js模块/导出系统:是否可以将模块导出为函数 [英] node.js module/export system: is it possible to export a module as a function
问题描述
我想在Dispatch.js中做这样的事情
I want to do something like this in Dispatch.js
function handle(msg) {
....
}
exports = handle;
这是在调用index.js
and this in the calling index.js
var dispatch = require("./Dispatch);
dispatch("data");
任何想法?
推荐答案
exports = handle
这会创建一个名为 exports
的局部变量。这与覆盖<$ c不同$ c> module.exports 。
This creates a local variable called exports
. This is different from overwriting module.exports
.
module.exports = handle
这会覆盖模块范围内的exports变量,然后由 require
读取。
This overwrites the exports variable that lives in module scope, this will then be read by require
.
在浏览器窗口[foo]
和 foo
是相同的,但是在节点模块[foo]
和 foo
的行为略有不同。
In the browser window["foo"]
and foo
are the same, however in node module["foo"]
and foo
behave subtly different.
局部变量范围上下文和模块
不是一回事。
The local variable scope context and module
are not the same thing.
这篇关于node.js模块/导出系统:是否可以将模块导出为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!