gpt4 book ai didi

javascript - 简单的 JavaScript : How to invoke a function based off user prompt;

转载 作者:行者123 更新时间:2023-11-28 00:07:46 24 4
gpt4 key购买 nike

我已经定义了一些函数,并且我想获取用户输入来调用这些函数。我有以下代码,但无法弄清楚当我使用变量时如何调用实际函数。我假设下面的代码可以工作..

谢谢!

var someFunctions = {
play: function() {
if (player.stopped()) {
player.play();
} else {
return false;
}
}


var getCommand = function(){

var command = prompt("Please enter a command");

if (!(command in someFunctions)) {
alert('command not recognized');
getCommand();
} else {
command();
}

}

getCommand();

最佳答案

var someFunctions = {
play: function() {
if (player.stopped()) {
player.play();
}
else {
return false;
}
}
};

var getCommand = function(){
var commandInput = prompt("Please enter a command");
var command = someFunctions[commandInput];

if (!command) {
alert('command not recognized');
getCommand();
}
else {
command();
}
};

getCommand();

关于javascript - 简单的 JavaScript : How to invoke a function based off user prompt;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31196186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com