- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
首先我应该指出,我对 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 函数。您可能希望 cookie
存在于 cmd
上,但这不是您的问题所问的。
我认为你最终会慢慢做一些真正的面向对象的事情,你会 have a constructor set up all the properties for your functions within functions 。也就是说,您需要保留 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
<小时/>
编辑:这里有一些关于如何使用this
的好信息:
this
关键字如何工作?”快速掌握的是 this
引用函数的执行上下文,如 the SO answer quotes ...
The ECMAScript Standard defines this as a keyword that "evaluates to the value of the ThisBinding of the current execution context" (§11.1.1).
因此,如果您没有附加任何内容到对象,则 window
(或者您的全局上下文是什么;在浏览器中,它是 window
)是 这个
。除非您使用严格模式...但否则 this
是调用对象。例如,当调用 bar
时,foo.bar()
应该在 this
中包含 foo
。
调用函数时,您可以使用函数 call
和 apply
显式设置 this
中的上下文。但这一切都在这些链接中进行了详细解释。
对于如何使用面向对象,我可能会做类似的事情
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 等。如果您正在进行单例设置,可能有点矫枉过正,但重新排列成真正的单例设置也不难。
关于javascript - 如果方法存在则调用该方法的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830086/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!