javascript - 修改类的一个实例的属性时,该类的所有实例上的该属性都会更改
问题描述
我的背景主要是 Python 和 C#,但我正在帮助一位正在学习 JavaScript 初级课程的朋友学习编码。这节课本周结束了,但它从未涉及如何在 JavaScript 中进行类,所以我想我应该快速拼凑一个例子来说明它们是如何工作的。唯一的问题是,每当我更改类的一个实例的属性值时,该属性在所有实例上都会更改。有没有办法使这项工作?
基本上当我对巨魔 1 造成伤害时,巨魔 2 的生命值仍应为 10(反之亦然)。相反,如果我对巨魔 1 造成 3 点伤害,巨魔 2 和巨魔 3 的生命值也会变为 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>
解决方案
您需要将break
语句放在switch语句中,否则它们都会被调用:
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()
}
推荐阅读
- google-maps - Flutter - 在同一屏幕上显示 Admob 广告和谷歌地图
- python - 如何定义 set 类型的新变量并将单词的文本列表存储在其中?
- networking - 什么是谷歌云休息 API 中的网络 URI
- webpack - Webpack mini-css-extract-plugin @font-face url 解析问题
- asp.net-core - NetCore API 模拟
- flutter - 无法在 GoogleSignIn 中捕获 Flutter 异常
- r - 如何使用 R 中的 gtsummary 包为表中的标签添加下标?
- unit-testing - 在 Azure DevOps 上,我找不到解释如何将单元测试项目作为 .NET Core 构建管道的一部分自动运行的示例
- python - pyshp 在 shapefile 中生成随机点
- clojure - 重定向后?不在解放者中工作或预检响应不成功