javascript - 如果没有足够的钱,我会一直得到 NaN,而不是登录到控制台并返回
问题描述
var Business = function(income, upkeep, level, upgradeCost, inc1, out1) {
this.income = income;
this.upkeep = upkeep;
this.level = level;
this.upgradeCost = upgradeCost;
this.change = function() {
money = money + this.income - this.upkeep
};
this.upgrade = function() {
var currMon = money;
if (currMon > this.upgradeCost) {
money = currMon - this.upgradeCost;
if (this.income === 0) {
this.income = this.income = inc1;
this.upkeep = this.upkeep * out1;
} else {
this.income = this.income * 1.05;
this.upkeep = this.upkeep * 1.05;
}
this.level++;
this.upgradeCost = this.level * 32 + (100 * this.income);
} else {
return;
}
};
如果没有足够的钱,当我执行.upgrade();
它时将钱更改为NaN
.
解决方案
我认为问题在于您使用money
而this.money
不是初始化this.money
为零或您要使用的任何默认值。我试着跑步
let bi = new Business(8000,90,1,100,1,2)
bi.upgrade()
在 Node.js 中并收到此错误:
ReferenceError: money is not defined
at Business.upgrade (repl:7:45)
推荐阅读
- javascript - 如何获得我单击以显示在下拉列表下方的下拉值
- reactjs - sx 和三元运算符 --MUI
- python - 使用 Python Graphviz 包设置自定义节点颜色
- vue.js - 如何在 Store Vuex 中加载 DOM 之前调用 api?
- azure-pipelines - 如何使用 DownloadPipelineArtifact 下载最后一次成功的构建
- for-loop - 如何遍历字典的所有值并将键附加到多个列表?
- flutter - 为什么发送以下请求会导致未捕获的异常?
- sharepoint - SP 库上的 SharePoint Permisison 已删除,我将如何恢复它?
- powershell - 从目录复制 PDF 并包含子目录
- python - Pandas 数据透视表,布局错误