我不断收到 - 无法读取未定义的属性(读取“toString”)[已关闭]

问题描述 投票:0回答:0

我尝试创建一个工作浏览器计算器,但是当我尝试选择任何数字或函数时,脚本失败,我最终得到

Uncaught TypeError: Cannot read properties of undefined (reading 'toString')at Calc.getDisplayNumber (Script.js:60:37)at Calc.updateDisplay (Script.js:78:16)at HTMLButtonElement.<anonymous> (Script.js:101:14)

完整项目 - https://github.com/Hellfire83/Calculator.git

getDisplayNumber(number) {
        const stringNumber = number.toString()
        const integerDigits = parseFloat(stringNumber.split('.')[0])
        const decimalDigits = stringNumber.split('.')[1]
        let integerDisplay
        if (isNaN(integerDigits)) {
          integerDisplay = ''
        } else {
          integerDisplay = integerDigits.toLocaleString('en', { maximumFractionDigits: 0 })
        }
       if (decimalDigits != null) {
        return `${integerDisplay}.${decimalDigits}`
      } else {
        return integerDisplay
      }
      }

    updateDisplay() {
        this.currentOperandTextElement.innerHTML =
          this.getDisplayNumber(this.currentOperand)
        if (this.operation != null) {
          this.previousOperandTextElement.innerHTML =
            `${this.getDisplayNumber(this.previousOperand)} ${this.operation}`
        } else {
          this.previousOperandTextElement.innerHTML = ''}
    }

从我非常有限的知识来看,错误似乎出在我的代码的

getDisplayNumber
updateDisplay
部分。

非常感谢任何帮助

javascript calculator tostring
© www.soinside.com 2019 - 2024. All rights reserved.