Javascript JS错误(需要类、接口或枚举)
我是JS的新手,正在尝试制作一个石头、布、剪刀的游戏 它不断地出现这个错误Javascript JS错误(需要类、接口或枚举),javascript,Javascript,我是JS的新手,正在尝试制作一个石头、布、剪刀的游戏 它不断地出现这个错误 error: class,interface, or enum expected 这是我的密码: function referee(){ var training = {}; function learn(winner,loser){ if (!training[winner]) training[winner] = {}; training[winner][lose
error: class,interface, or enum expected
这是我的密码:
function referee(){
var training = {};
function learn(winner,loser){
if (!training[winner]) training[winner] = {};
training[winner][loser]=1;
}
function judge(play1,play2){
if (play1 === play2){ return "tie"; }
return ( (training[play1][play2] === 1)? play1: play2 )+ "wins!";
}
function validate(choice) {
return choice in training;
}
function choices() {
return Object.keys(training);
}
return {
"learn": learn,
"judge": judge,
"validAction": validate,
"getChoices": choices
};
}
var ref = referee();
ref.learn("rock","scissors");
ref.learn("paper","rock");
ref.learn("scissors","paper");
do {
var userChoice = prompt("Do you choose rock, paper or scissors?");
} while(!ref.validAction(userChoice))
var choices = ref.getChoices(),
computerChoice = choices[Math.floor(Math.random()*choices.length)];
console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
console.log(ref.judge(userChoice, computerChoice));
Object.key(训练)代码>仅在Chrome中受支持
改变
function choices() {
return Object.keys(training);
}
到
您可能希望使其成为一个函数,而不是将return放在末尾。现在,您的“ref”变量包含一个数组,因为这是仲裁函数返回的结果
我看到希瑟姆·S·阿尔卡德希布已经击败我找到了答案,哈哈 我看到您正在将reference
用作一个类,而它没有被解释为一个类。请参考指南了解js中的OO概念。
function choices() {
var keys = [];
for(var key in this) keys.push(key);
return keys;
}