javascript - Node module.exports pattern - Is this correct? -


i'm new js , working on irc bot. have produced following module want use creating bot commands.

 /***  * module contains available bot commands including  * access levels view/utilise command , text.  *   * usage example: commands.tg.play(payload);  *  ***/ module.exports = commands = {    tg: {     reg: '^[. - !]tg',     help: 'some text tg',     play: function(payload){tg(payload);}   },   help: {     reg: '^[. - !]help',     description: 'some text 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: ';   (var in commands){     output=output+ ' ' + i.tostring();   }  return console.log(output); } 

as can see defining methods within commands object. in order try , keep things bit cleaner define functions run below commands object. can access these via commands.help.play(payload). wanted know whether there better way or direction going correct? @ moment commands skeleton , carrying out quite bit more work wanted post give general idea.

i don't fn call you're making: play:function(payload){tg(payload);} should play:tg since functions first-order citizens in js. prefer assign module.exports @ end of file.

/***  * module contains available bot commands including  * access levels view/utilise command , 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: ';   (var in commands){     output=output+ ' ' + i.tostring();   }  return console.log(output); }   // export module.exports = commands = {    tg: {     reg: '^[. - !]tg',     help: 'some text tg',     play: tg;   },   help: {     reg: '^[. - !]help',     description: 'some text intel',     play: help}   }  }; 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -