节点模块导出模式 - 这是正确的吗?
Node module.exports pattern - Is this correct?
我是JS的新手,我目前正在研究IRC机器人。我制作了以下模块,我想使用它来创建机器人命令。
/***
* Module contains available bot commands including
* access levels to view/utilise command and help text.
*
* Usage example: commands.tg.play(payload);
*
***/
module.exports = commands = {
tg: {
reg: '^[. - !]tg',
help: 'some help text for tg',
play: function(payload){tg(payload);}
},
help: {
reg: '^[. - !]help',
description: 'some help text for intel',
play: function(payload){help(payload);}
}
};
function tg(payload){
//Example: msg_route: pm msg_from: munkee msg_data: .tg munkee testing message via tg command msg_match: .tg msg_method: .
console.log("msg_route:" + payload.msg_route + ' msg_from: ' + payload.msg_from + ' msg_data: ' + payload.msg_data + ' msg_match: ' + payload.msg_match + ' msg_method: ' + payload.msg_method);
return;
}
function help(payload){
var output='Available commands: ';
for (var i in commands){
output=output+ ' ' + i.toString();
}
return console.log(output);
}
如您所见,我目前正在命令对象中定义一些方法。为了尝试使事情更干净一些,我定义了要在命令对象下实际运行的函数。我可以通过command.help.play(payload)轻松访问这些。但是,我想知道是否有更好的方法来做到这一点,或者我前进的方向是否正确?目前,这些命令非常骨架,将执行更多的工作,但我只是想发布一些东西来给出总体思路。
我不喜欢你所做的额外fn调用:play:function(payload){tg(payload);}
应该只是play:tg
,因为函数是js中的一阶公民。我也更喜欢在文件末尾分配给module.exports。
/***
* Module contains available bot commands including
* access levels to view/utilise command and help text.
*
* Usage example: commands.tg.play(payload);
*
***/
var commands = {};
function tg(payload){
//Example: msg_route: pm msg_from: munkee msg_data: .tg munkee testing message via tg command msg_match: .tg msg_method: .
console.log("msg_route:" + payload.msg_route + ' msg_from: ' + payload.msg_from + ' msg_data: ' + payload.msg_data + ' msg_match: ' + payload.msg_match + ' msg_method: ' + payload.msg_method);
return;
}
function help(payload){
var output='Available commands: ';
for (var i in commands){
output=output+ ' ' + i.toString();
}
return console.log(output);
}
// export
module.exports = commands = {
tg: {
reg: '^[. - !]tg',
help: 'some help text for tg',
play: tg;
},
help: {
reg: '^[. - !]help',
description: 'some help text for intel',
play: help}
}
};
这是关于我个人的偏好,但我会选择这个。带有构造函数的单例模式
function Commands(){
this.commands = {};
this.tg = function (payload){
//Example: msg_route: pm msg_from: munkee msg_data: .tg munkee testing message via tg command msg_match: .tg msg_method: .
console.log("msg_route:" + payload.msg_route + ' msg_from: ' + payload.msg_from + ' msg_data: ' + payload.msg_data + ' msg_match: ' + payload.msg_match + ' msg_method: ' + payload.msg_method);
return;
};
this.help = function (payload){
var output='Available commands: ';
for (var i in commands){
output=output+ ' ' + i.toString();
}
}
this.commands.tg= {
reg: '^[. - !]tg',
help: 'some help text for tg',
play: this.tg
};
this.commands.help= {
reg: '^[. - !]help',
description: 'some help text for intel',
play: this.help
};
}
if(!!obj)
obj = new Commands();
module.exports = obj;
相关文章:
- Javascript,访问一个主要对象模块模式中的每个对象
- 模块模式和这个
- 显示模块模式在Knockout中设置模型的新实例
- Javascript中的模块模式和揭示模块模式是否仅在创建API时有用;s
- JavaScript模块模式-如何在使用对象/函数之前激发构造函数/init函数
- TypeScript代码类似于揭示模块模式结构
- 为什么在javascript中的模块模式中实现Lazy函数时范围会发生变化
- JavaScript:模块模式差异
- 显示模块模式中的私有成员
- 如何将window.setTimeout与javascript和模块模式一起使用
- j查询模块模式未命名 |如何访问“$”
- 挖空和显示模块模式的数据绑定问题
- 传递参数时如何避免模块模式中的 getter/setter 函数
- JavaScript 模块模式给出了意想不到的结果
- 从模块模式开始
- 试图通过模块模式在DOM元素上实现change()事件
- 揭示模块模式、KnockoutJS和CoffeeScript
- 使用模块模式时访问父作用域中的变量
- 模块模式:函数未定义
- 如何用模块模式扩展javascript中的事件委派