我努力学习超越javascript绝对基础的东西。我一直在关注一个教程,并且他们正在展示他们所展示的基础知识,但我试图加强它并且没有太多运气。我使用名为Hackmud的游戏作为学习/开发JS技能的工具。这就是我所拥有的和我正在尝试的......
基本上,我在终端类型窗口中运行命令,解析输出,然后根据这些结果执行另一个命令。
function (context, args) {
function stringFormat(stringToFormat) {
return stringToFormat.split('\n').join(' ')
}
function search(regex, response) {
let matches = regex.exec(response)
//let match2 = regex.exec(response)
let arr = []
while (matches) {
if (matches.index === regex.lastIndex) {
regex.lastIndex++
}
// Replace eliminates trailing , or . punctuation
arr.push(matches[1].replace(/([.,])+$/, "") || matches[2])
matches = regex.exec(response)
}
// return arr
return [...new Set(arr)]
}
let page = {}
let response = stringFormat(args.target.call())
let sitePages = search(/\s(\w+)\s\|/g, response)
response = stringFormat(args.target.call({}))
let siteNav = search(/\s(\w+)\:"(\w+)/g, response)
let siteKey = search(/\s\w+:"(\w+)/g, response)
// let wallPat = /\s(\w+)\:"(\w+)/g
// let siteNav = wallPat.exec(response)
// Captures password for site
page[siteNav] = sitePages[1]
response = args.target.call(page)
let sitePass = search(/(?:strategy\s)(\S+)/g, response)
page[siteNav] = sitePages[0]
response = args.target.call(page)
// Regex captures project names from "of projects, ment on, and nues on" for developments and continues
// Need to eliminte trailing punctuation if there.
let siteProjects = search(/(?:of\sproject\s|ments\son\s|nues\son\s)(\S+)/g, response)
// return args.target.call(page)
return [["`NTarget:`", siteKey], ["`NPasswd:`", sitePages[1]], ["`NProjects:`", siteProjects], ["`NPasswd:`", sitePass]]
}
这基本上归还给我一些细节,我试图用它来进入游戏过程的下一步。问题是下一步将涉及将我收集的几个关键:值对传递给args.target.call()。这是我难倒的地方。我一直在研究,但我想我错过了一些阻碍我找到所需内容的关键概念。
在游戏上下文中,正在运行的命令如下所示:
>>tyrell.public { get:"description" }
Want something new and original? Go somewªere else! Taco Tyrell's has been replicating Tyrell's Perfect Taco down to the molecul¦ for sixty years. Try it! You'll Like It Exactly As Much As You Did The First Time.(tm)
We are calling this strategy bethebest and we will continue to strive to deliver on this promise.
下一步涉及执行如下命令:
>>tyrell.public { get:"list", p:"bethebest", project:"forgetme_nt" }
我真的迷失在这里如何传递调用()这些多个键:值对。任何帮助将不胜感激。
答案 0 :(得分:0)
嗯,这取决于功能实现。
参数本身可以解析为JSON,但调用方法并不能通过键/值对实际工作,而只能通过位置参数。因此,您无法将get
分配给名为get
的参数,因为该信息(据我所知)并未存储在任何位置。
如果实现此函数,则可以接受包含普通对象的通用options
参数,就像在命令提示符中输入的那样。然后你的函数可以迭代对象键或用它做任何事情。