javascript - 在 JavaScript 中调用类内部的方法
问题描述
我目前正在学习 JavaScript,并且我编写了一些仅使用基本功能的代码,但现在我正在尝试将我的代码转换为一个类,并且除了 1 件事之外我得到了一切工作。我正在尝试从类中的函数中调用函数:
class Donut {
constructor(
donutCountSpan,
autoClickerPriceSpan,
autoClickerCountSpan,
donutMultiplierCountSpan,
donutMultiplierPriceSpan
) {
this.donutCountSpan = donutCountSpan;
this.autoClickerPriceSpan = autoClickerPriceSpan;
this.autoClickerCountSpan = autoClickerCountSpan;
this.donutMultiplierCountSpan = donutMultiplierCountSpan;
this.donutMultiplierPriceSpan = donutMultiplierPriceSpan;
this.donutCount = 200;
this.donutMultiplierCount = 0;
this.autoClickerCount = 0;
this.autoClickerPrice = 100;
this.donutMultiplierCount = 0;
this.donutMultiplierPrice = 10;
}
activateAutoClickers() {
setInterval(function () {
this.donutCount +=
this.autoClickerCount * Math.pow(1.2, this.donutMultiplierCount);
this.donutCountSpan.innerText = Math.round(this.donutCount);
}, 1000);
}
buyAutoClicker() {
if (this.donutCount >= this.autoClickerPrice) {
this.donutCount -= this.autoClickerPrice;
this.autoClickerCount += 1;
this.autoClickerPrice = Math.round(this.autoClickerPrice * 1.1);
this.donutCountSpan.innerText = Math.round(this.donutCount);
this.autoClickerPriceSpan.innerText = this.autoClickerPrice;
this.autoClickerCountSpan.innerText = this.autoClickerCount;
if (this.autoClickerCount <= 1) {
this.activateAutoClickers(); //THIS IS THE METHOD THAT ISN'T BEING CALLED
}
}
}
这是我的按钮的事件侦听器:
donut.addEventListener("click", () => {
donutClicker.addDonut();
});
buyAutoClickerButton.addEventListener("click", ()=> {
donutClicker.buyAutoClicker();
});
buyDonutMultiplierButton.addEventListener("click", ()=> {
donutClicker.buyDonutMultiplier();
});
解决方案
推荐阅读
- c - 如何避免在函数调用中保留 C 中的字符串值
- sqlite - 在 Android 中获取 SQLite 结果的正确方法
- android - 如何在android中实现实时动态图标,如时钟/日历应用程序?
- javascript - 拦截任何请求 - javascript
- python - 从 Azure Databricks 中的 Azure Datalake Gen2 读取 .nc 文件
- antd - Ant Design 异步树的功能重构返回关键错误
- c++ - 找不到 Boost 静态库:Log_Setup 和 Thread
- xcode - 无法从项目标头 Xcode 11 访问变量或类型
- python - 如果我在 TCP 通信中丢失了数据包,如何将我的客户端重新连接到我的服务器?
- php - Android没有将图像/位图上传到服务器