javascript - 无法设置未定义的属性 'max'

标签 javascript typescript

我正在使用 typescript 制作一个骰子显示应用程序,我对它的学习还很陌生,但我认为我学得很好,但是我一直在用我正在工作的这个骰子应用程序来解决这个问题在。该应用程序的目的是掷骰子,最小数为 1,最大数在窗口负载上设置,当前为 99,然后在索引上生成并显示随机数以及最小和最大值。但是,当我编译并测试它时,我在控制台中收到此错误:

main.ts:66 Uncaught TypeError: Cannot set property 'max' of undefined at window.onload (main.ts:66)

window.onload @ main.ts:66 加载(异步)

(匿名)@ main.ts:63

在 vsCode 的控制台中,我在 main.ts 文件中得到了这个 errors in console

"use strict";
class Dice {
  constructor(max) {
    this.max = max;
    this.min = 1;
  }
  get _max() {
    return this.max;
  }
  get _min() {
    return this.min;
  }
  set _max(max) {
    this.max = max;
  }
  set _min(min) {
    this.min = min;
  }
  roll() {
    return Math.floor(Math.random() * this.max) + this.min;
  }
}
class Display {
  constructor(vm) {
    this.max = vm.max.toString();
    this.min = vm.min.toString();
    this.rand = vm.rand.toString();
  }
  update() {
    document.getElementById("random").innerText = this.rand;
    if (this.min !== undefined) {
      document.getElementById("min").innerText = this.min;
    }
    if (this.max !== undefined) {
      document.getElementById("max").innerText = this.max;
    }
  }
}
window.onload = () => {
  let ukDice = new Dice(99);
  let viewModel;
  viewModel.max = ukDice._max;
  viewModel.min = ukDice._min;
  viewModel.rand = ukDice.roll();
  let output = new Display(viewModel);
  output.update();
};
//# sourceMappingURL=main.js.map
<!DOCTYPE html>
<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
  <div id="random"></div>
  <div id="min"></div>
  <div id="max"></div>
</body>

</html>

Main.ts:

interface viewModel {
  max: number;
  min: number;
  rand: number;
}

class Dice {

  private max: number;
  private min: number;

  constructor(max: number) {
    this.max = max;
    this.min = 1;
  }

  get _max(): number {
    return this.max;
  }

  get _min(): number {
    return this.min;
  }

  set _max(max: number) {
    this.max = max;
  }

  set _min(min: number) {
    this.min = min;
  }

  roll() {
    return Math.floor(Math.random() * this.max) + this.min;
  }
}

class Display {

  private max: string;
  private min: string;
  private rand: string;

  constructor(vm: viewModel) {
    this.max = vm.max.toString();
    this.min = vm.min.toString();
    this.rand = vm.rand.toString();
  }

  update() {
    document.getElementById("random").innerText = this.rand;

    if (this.min !== undefined) {
      document.getElementById("min").innerText = this.min;
    }

    if (this.max !== undefined) {
      document.getElementById("max").innerText = this.max;
    }
  }
}

window.onload = () => {
  let ukDice = new Dice(99);
  let viewModel: viewModel;
  viewModel.max = ukDice._max;
  viewModel.min = ukDice._min;
  viewModel.rand = ukDice.roll();
  let output = new Display(viewModel);
  output.update();
}

最佳答案

您需要声明为 let viewModel = new Display(ukDice); 并在 Dice 中添加 this.rand = 1;构造函数

"use strict";
class Dice {
  constructor(max) {
    this.max = max;
    this.min = 1;
    this.rand = 1;
  }
  get _max() {
    return this.max;
  }
  get _min() {
    return this.min;
  }
  set _max(max) {
    this.max = max;
  }
  set _min(min) {
    this.min = min;
  }
  roll() {
    return Math.floor(Math.random() * this.max) + this.min;
  }
}
class Display {
  constructor(vm) {
    this.max = vm.max.toString();
    this.min = vm.min.toString();
    this.rand = vm.rand.toString();
  }
  update() {
    document.getElementById("random").innerText = this.rand;
    if (this.min !== undefined) {
      document.getElementById("min").innerText = this.min;
    }
    if (this.max !== undefined) {
      document.getElementById("max").innerText = this.max;
    }
  }
}
window.onload = () => {
  let ukDice = new Dice(99);
  let viewModel = new Display(ukDice);
  viewModel.max = ukDice._max;
  viewModel.min = ukDice._min;
  viewModel.rand = ukDice.roll();
  let output = new Display(viewModel);
  output.update();
};
//# sourceMappingURL=main.js.map
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="random"></div>
<div id="min"></div>
<div id="max"></div>

关于javascript - 无法设置未定义的属性 'max',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57619983/

相关文章:

reactjs - 代码覆盖率 Cypress 和 Storybook,无法检测我的代码

typescript - 如何避免在 Typescript 中分布多个泛型类型参数?

javascript - 自动完成不返回输出

javascript - 如何删除jquery数组中的部分重复值

javascript - 在不同的类中使用另一个 javascript 类

typescript - 使用数据库的环回 4 身份验证

javascript - 在 TypeScript 中实现接口(interface)原型(prototype)

Angular on keyup.enter 将输入的当前文本发送到方法

javascript - protected HTML/CSS for js?我无法在运行时更改某些属性

javascript - 我可以用 JavaScript 检测我们在 CSS 关键帧动画中的百分比明智吗