调用方法,如果它存在

首先,我要指出的是,我在JS和node.js中都是新手。 我正在尝试编写一个node.js聊天机器人,目前正在使用命令。

我的问题是调用方法的方法..如果这是可能的。 这是相关部分:

var text; // received message from user var userId; // Id of the user, number. if (cmd.hasOwnProperty(text.split(' ')[0])) { // Message will be a string, it should be ignore if the first word is not a command name. console.log("[Inc]" + userId + ": " + text) if (text.split(' ').length === 1) { // If the command name is not followed by any arguments then the only argument passed should be the userID(used to send replies to the user). cmd[text.split(' ')[0]](userId) } else { var args = [] // Placing all args in an array because the commands will take different number of arguments, a calculator command for example could take a lot of args. for (var i = 1; i < text.split(' ').length; i++) { args.push(text.split(' ')[i]) } console.log(args) cmd[text.split(' ')[0]](userId, args) } } else { console.log("Command not found, try again"); } var cmd = { help : function () { cookies : function () { // console.log('Cookies recipe') } if (arguments.length === 1) { console.log('General help text'); } else if (help.hasOwnProperty(arguments[1])) { this[arguments[0]](); // Seems my problem was here, this was creating the reference error. } else { console.log('Requested topic not found') } }, invite : function(id) { send_PRIVGRP_INVITE(id) } } 

任何想法,我可以做这个工作,或者有更好的方法,更干净的方式来做到这一点。 另外我应该提到,我select使用命令作为对象和方法,因为一些命令会更复杂,我打算把它们放在file.js上,并把它导出到main.js文件中,添加起来会容易很多命令,而不用编辑主文件。

请记住,我很新,在这方面,详细的解释会有很长的路要走,谢谢。

我认为下面的代码给出你正在寻找的行为,但它是非常反模式。 这是一种面向对象的方式,但实际上并不是这样,因为它在包含函数的每次调用中都以同样的方式定义了cookies函数。 你可能想让cookies生活在cmd ,但这不是你的问题所要求的。

我想你最终会考虑做一些真正的面向对象的东西,你将有一个构造函数在函数内设置你的函数的所有属性 。 也就是说,你将要离开JSON表示法,并将其作为“真实代码”运行,返回一个对象构造函数,返回可能以不同方式初始化的cmd实例(也许使用JSON表示法!)。

如果这不是你想要的,请发表评论,我会修改。 再一次,我不会在生产中实际使用这个代码。 反模式。

 var cmd = { help: function () { var context = this.help; context.cookies = function () { console.log('Cookies recipe'); }; if (arguments.length === 0) { console.log('General help text'); } else if (context.hasOwnProperty(arguments[0])) { context[arguments[0]](); } else { console.log('Requested topic not found'); } }, invite: function(id) { //send_PRIVGRP_INVITE(id); console.log("send_PRIVGRP_INVITE(" + id + ");"); } }; cmd.help(); cmd.help("cookies"); cmd.help("milk"); cmd.invite("wack"); cmd.help("invite"); 

这将产生这个输出:

 General help text Cookies recipe Requested topic not found send_PRIVGRP_INVITE(wack); Requested topic not found 

编辑:这里有一些关于如何使用它的很好的信息:

  • MDN
  • 那么回答 “ this关键字是如何工作的?

快速回家就是this指的是函数的执行上下文 ,因为SO答案引用 …

ECMAScript标准将其定义为“评估当前执行上下文的ThisBinding的值”(§11.1.1)的关键字。

所以,如果你没有东西附加到一个对象, window (或者你的全局上下文是什么;在浏览器中,这是window )是this 。 除非你使用严格模式,否则this是调用对象。 foo.bar() ,当bar被调用时, foo.bar()应该有foo

您可以使用函数callapply来显式设置调用函数时所在的上下文。 但是这些链接都详细解释了这一点。

一个OO解决scheme

对于如何使用面向对象,我可能会做一些像…

 function Cmd(helpInfo, executableFunctions) { var functionName; // Note that we're no longer redefining the cookies function // with every call of cmd.help, for instance. this.help = function (helpTopic) { if (undefined === helpTopic) { console.log('General help text'); } else if (helpInfo.hasOwnProperty(helpTopic)) { console.log(helpInfo[helpTopic]); } else { console.log('Requested topic not found'); } }; for (functionName in executableFunctions) { if (executableFunctions.hasOwnProperty(functionName)) { this[functionName] = executableFunctions[functionName]; } } } // Set up initialization info for your new command object. var helpInfoCooking = { cookies: "Cookies recipe", brownies: "Brownies recipe", cake: "Cake receipe" }; var executableFunctions = { invite: function(id) { //send_PRIVGRP_INVITE(id); console.log("send_PRIVGRP_INVITE(" + id + ");"); }, say: function(message) { console.log("you'd probably say \"" + message + "\" somehow"); } }; // Create an instance of Cmd. var cmd = new Cmd(helpInfoCooking, executableFunctions); cmd.help(); cmd.help("cookies"); cmd.help("milk"); cmd.help("cake"); cmd.invite("wack"); cmd.help("invite"); cmd.say("This is a message"); 

结果:

 General help text Cookies recipe Requested topic not found Cake receipe send_PRIVGRP_INVITE(wack); Requested topic not found you'd probably say "This is a message" somehow 

YMMV等。如果你正在做一个单身设置,也许是过度杀伤,但是重新安排到真正的单身设置也不是那么难。