首页 > 解决方案 > 如果没有足够的钱,我会一直得到 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.

标签: javascript

解决方案


我认为问题在于您使用moneythis.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)

推荐阅读