节点模块导出模式 - 这是正确的吗?

Node module.exports pattern - Is this correct?

本文关键字:模块 模式 节点      更新时间:2023-09-26

我是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;