node.js模块/导出系统:是否可以将模块导出为函数 [英] node.js module/export system: is it possible to export a module as a function

查看:103
本文介绍了node.js模块/导出系统:是否可以将模块导出为函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆