Javascript Node.js解释器返回未定义的函数
我正在努力学习后端和前端语言。 最近,我一直在尝试用JavaScript实现Derek Banas的“OOPGame[C#Tutorial no.8]”。但是,当我使用节点解释器运行它时,代码返回“GetAttackResult not defined”。Javascript Node.js解释器返回未定义的函数,javascript,node.js,class,Javascript,Node.js,Class,我正在努力学习后端和前端语言。 最近,我一直在尝试用JavaScript实现Derek Banas的“OOPGame[C#Tutorial no.8]”。但是,当我使用节点解释器运行它时,代码返回“GetAttackResult not defined”。 我怎样才能修好它 这是我的密码: class Warrior { constructor(name, health) { this.name = name; this.health = heal
我怎样才能修好它 这是我的密码:
class Warrior
{
constructor(name, health)
{
this.name = name;
this.health = health;
}
Attack()
{
let attackValue = Math.round(Math.random * 50);
return attackValue;
}
Block()
{
let blockValue = Math.round(Math.random * 10);
return blockValue;
}
}
class Battle
{
StartFight(warrior1 , warrior2)
{
while (true)
{
if (GetAttackResult(warrior1, warrior2) == "Game Over")
{
console.log("Game Over");
break;
}
if (GetAttackResult(warrior2, warrior1) == "Game Over")
{
console.log("Game Over");
break;
}
}
}
GetAttackResult(warriorA, warriorB)
{
let warA_attack = warriorA.Attack();
let warB_block = warriorB.Block();
let dmg2warB = warA_attack - warB_block;
warriorB.health = warriorB.health - dmg2warB;
if (dmg2warB > 0)
{
console.log(`${warriorA.name} attacks ${warriorB.name} and deals ${dmg2warB} damage.`);
console.log(`${warriorB.name} has ${warriorB.health} health.\n`);
if (warriorB.health <= 0)
{
console.log(`${warriorB.name} has died and ${warriorA.name} is victorius`);
return "Game Over";
}
else return "Fight Again!";
}
}
}
Main();
function Main()
{
let apple = new Warrior("Apple", 3000);
let microsoft = new Warrior("Microsoft", 3000);
let hi = new Battle();
hi.StartFight(apple, microsoft);
}
职业战士
{
构造函数(名称、运行状况)
{
this.name=名称;
健康=健康;
}
攻击()
{
让attackValue=Math.round(Math.random*50);
返回攻击值;
}
块()
{
让blockValue=Math.round(Math.random*10);
返回块值;
}
}
阶级斗争
{
StartFight(warrior1,warrior2)
{
while(true)
{
如果(GetAttackResult(warrior1,warrior2)=“游戏结束”)
{
控制台日志(“游戏结束”);
打破
}
如果(GetAttackResult(warrior2,warrior1)=“游戏结束”)
{
控制台日志(“游戏结束”);
打破
}
}
}
GetAttackResult(warriorA、warriorB)
{
让warA_attack=warriorA.attack();
设warB_block=warriorB.block();
设dmg2warB=warA_攻击-warB_块;
warriorB.health=warriorB.health-dmg2warB;
如果(dmg2warB>0)
{
log(`${warriorA.name}攻击${warriorB.name}并造成${dmg2warB}伤害。`);
log(${warriorB.name}具有${warriorB.health}运行状况。\n`);
如果(warriorB.health在您的代码中,GetAttackResult
是一个方法。这意味着您可以在通过new Battle
创建的实例上作为属性访问它(通常是this
)。它不是一个独立的函数
例如(见评论):
某些语言允许您保留此项。
关闭对字段和方法(Java,C#)的引用。JavaScript不允许。如果您看到第二组^
指向没有此项的内容。
点击刷新。编辑错误,doh!
[Running] node "i:\JSPractise\tempCodeRunnerFile.js"
i:\JSPractise\tempCodeRunnerFile.js:4
let apple = new Warrior("Apple", 3000);
^
ReferenceError: Warrior is not defined
at Main (i:\JSPractise\tempCodeRunnerFile.js:4:17)
at Object.<anonymous> (i:\JSPractise\tempCodeRunnerFile.js:1:63)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
[Done] exited with code=1 in 0.42 seconds
StartFight(warrior1 , warrior2)
{
while (true)
{
if (this.GetAttackResult(warrior1, warrior2) == "Game Over")
// ^^^^^
{
console.log("Game Over");
break;
}
if (this.GetAttackResult(warrior2, warrior1) == "Game Over")
// ^^^^^
{
console.log("Game Over");
break;
}
}
}