gpt4 book ai didi

javascript - 当修改一个类的一个实例的属性时,该类的所有实例的属性都会改变

转载 作者:行者123 更新时间:2023-11-30 11:05:58 25 4
gpt4 key购买 nike

我的背景主要是 Python 和 C#,但我正在帮助一位正在参加 JavaScript 入门类(class)的 friend 学习编码。该类(class)本周结束,但它从未涉及如何用 JavaScript 编写类(class),所以我想我应该很快地拼凑一个例子来说明它们是如何工作的。唯一的问题是,每当我更改类的一个实例的属性值时,该属性在所有实例上都会更改。有什么办法可以做到这一点吗?

基本上当我对 troll1 造成伤害时,troll2 的生命值应该仍然是 10(反之亦然)。相反,如果我对 troll1 造成三点伤害,那么 troll2 和 troll3 的生命值也会变为 7。

我曾尝试在构造函数之外设置健康,但随后在调用类方法时出现错误,提示未定义健康(无论我使用 this.health 还是仅使用健康)。

这是我做的例子的html和js:

class trollEnemy {

constructor() {
this.health = 10;
}


takeDamage(damageAmount) {
this.health = this.health - damageAmount;
if (this.health > 0) {
document.getElementById("outputDiv").innerHTML = "Dealt " + damageAmount + " damage to the troll, the troll now has " + this.health + " health left";
} else {
document.getElementById("outputDiv").innerHTML = "Dealt " + damageAmount + " damage to the troll, the troll is now dead";
}
}

getHealth() {
if (this.health > 0)
document.getElementById("outputDiv").innerHTML = "The troll has " + this.health + " health left";
else
document.getElementById("outputDiv").innerHTML = "The troll is dead";
}

}

var troll1 = new trollEnemy();
var troll2 = new trollEnemy();
var troll3 = new trollEnemy();

function generateNewTroll(trollNumber) {
switch (trollNumber) {
case 1:
troll1 = new trollEnemy();
case 2:
troll2 = new trollEnemy();
case 3:
troll3 = new trollEnemy();
}
}

function damageTroll(trollNumber) {
switch (trollNumber) {
case 1:
troll1.takeDamage(document.getElementById("trollDamageAmount").value);
case 2:
troll2.takeDamage(document.getElementById("trollDamageAmount").value);
case 3:
troll3.takeDamage(document.getElementById("trollDamageAmount").value);
}
}

function checkTrollHealth(trollNumber) {
switch (trollNumber) {
case 1:
troll1.getHealth();
case 2:
troll2.getHealth();
case 3:
troll3.getHealth();
}
}
<button onclick="generateNewTroll(1)">Generate New Troll #1</button><button onclick="damageTroll(1)">Deal Damage To Troll #1</button> <button onclick="checkTrollHealth(1)">Check Troll #1 Health</button><br>
<button onclick="generateNewTroll(2)">Generate New Troll #2</button><button onclick="damageTroll(2)">Deal Damage To Troll #2</button> <button onclick="checkTrollHealth(2)">Check Troll #2 Health</button><br>
<button onclick="generateNewTroll(3)">Generate New Troll #3</button><button onclick="damageTroll(3)">Deal Damage To Troll #3</button> <button onclick="checkTrollHealth(3)">Check Troll #3 Health</button> Enter the amount of damage you want to deal to a
troll: <input type="text" id="trollDamageAmount">

<br>

<div id="outputDiv">Test</div>

最佳答案

您需要在switch 中加入break 语句声明,否则他们都会被调用:

switch(trollNumber)
{
case 1:
troll1.getHealth();
break
case 2:
troll2.getHealth();
break
case 3:
troll3.getHealth();
break
}

没有中断的事情通过例如:

let trollNumber = 1
switch(trollNumber)
{
case 1:
console.log(1)
case 2:
console.log(2)
case 3:
console.log(3)
}

如果将巨魔存储在数组中,您可能会更快乐。这将简化一切。例如,您可以编写如下函数:

function damageTroll(trollNumber){
trolls[trollNumber].getHealth()
}

关于javascript - 当修改一个类的一个实例的属性时,该类的所有实例的属性都会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55602253/

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