[英]Call the method of a method if it exists
首先,我应该指出,我是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)
}
}
关于如何进行此工作的任何想法,或者是否有更好的方法,更干净的方法来执行此操作。 我还要提到我选择使用命令作为对象和方法,因为有些命令会更复杂,并且我打算将它们放在file.js上并将其导出到main.js文件中,添加起来会容易得多命令始终不编辑主文件。
请记住,我对此很陌生,详细的解释将有很长的路要走,谢谢。
我认为以下代码提供了您正在寻找的行为,但这是非常反模式的。 这是一种面向对象的外观 ,但实际上不是,因为它在包含函数的每次调用中都以相同的方式定义了cookies
函数。 您可能希望cookies
驻留在cmd
,但这不是您的问题所要问的。
我认为您最终会精打细算地做一些面向对象的工作,在这里您将有一个构造函数来设置函数中函数的所有属性 。 也就是说,您将需要保留JSON表示法,并在对象构造函数中以“真实代码”的形式运行它,以返回可以用不同方式初始化(可能使用JSON表示法!)的cmd
实例。
如果这不是您想要的,请发表评论,我会进行修改。 同样,我不会在生产中实际使用此代码。 反模式。
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
编辑:有一个关于如何使用一些好的信息, this
在这里:
快速了解一下, this
是指函数的执行上下文 ,因为SO答案引用了 ...
ECMAScript标准将其定义为“评估为当前执行上下文的ThisBinding的值”(第11.1.1节)的关键字。
因此,如果您没有在对象, window
(或任何全局上下文;在浏览器中是window
)上附加任何内容,则为this
。 除非您使用严格模式...否则,否则this
就是调用对象。 foo.bar()
应该有foo
在this
时候bar
被调用,例如。
您可以使用函数call
,并apply
于明确地设置这是在上下文this
调用函数时。 但这一切都在那些链接中进行了详细说明。
对于如何使用OO,我可能会做类似...
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,等等。如果您正在执行单例设置,则可能会过大,但也可以很容易地将其重新排列为真正的单例设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.