简体   繁体   English

为什么我的代码在控制台中显示“Uncaught SyntaxError: Identifier 'calculator' has already been declared'?

[英]why is my code saying' Uncaught SyntaxError: Identifier 'calculator' has already been declared' in the console?

I tried making a calculator, but checking it at this stage, it's displaying some syntax error.我试着制作一个计算器,但在这个阶段检查它,它显示了一些语法错误。

Please I need help.我需要帮助。 How can I correct this?我该如何纠正这个问题? There is a class of calculator there and I represented it with the 'new' word.那里有一个 class 的计算器,我用“新”这个词来表示它。 This is vanilla JavaScript. Tried calling the function, but it kept on giving me the error in my console.这是香草 JavaScript。尝试调用 function,但它一直在我的控制台中给我错误。 Please could someone help me?请问有人可以帮助我吗?

class calculator {
  constructor(previousOperandTextElement, currentOperandTextElement) {
    this.perviousOperandTextElement = previousOperandTextElement
    this.currentOperandTextElement = currentOperandTextElement
    this.clear()
  }
    
  clear() {
    this.currentOperand = ''
    this.previousOperand = ''
    this.operation = undefined
  }
    
  delete() {
  }

  appendNumber(number) {
    this.currentOperand = number
  }
    
  chooseOperation(operation) {   
  }
    
  compute() {
  }

  updateDisplay() {
    this.currentOperandTextElement.innerText = this.currentOperand;
  }
}
    
const numberButtons = document.querySelectorAll('[data-number]')
const operationButtons = document.querySelectorAll('[data-operation]')
const equalsButton = document.querySelector('[data-equals]')
const deleteButton = document.querySelector('[data-delete]')
const allclearButton = document.querySelector('[data-all-clear]')
const previousOperandTextElement = document.querySelector('[data-previous-operand]')
const currentOperandTextElement = document.querySelector('[data-current-operand]')
        
const calculator = new calculator(previousOperandTextElement, currentOperandTextElement)

numberButtons.forEach(button => {
  button.addEventListener('click', () => {
    calculator.appendNumber(button.innerText)
    calculator.updateDisplay()
  })
})

Identifier 'calculator' has already been declared' means you've used name 'calculator' twice, in your code - class is named 'calculator' and constant for calculator object is also 'calculator'.标识符'calculator' has already been declared' 表示您在代码中使用了名称'calculator' 两次- class 被命名为'calculator' 并且计算器常量object 也是'calculator'。

Change class name to 'Calculator':将 class 名称更改为“计算器”:

class Calculator {
  constructor(previousOperandTextElement, currentOperandTextElement) {
    this.perviousOperandTextElement = previousOperandTextElement
    this.currentOperandTextElement = currentOperandTextElement
    this.clear()
  }

  clear() {
    this.currentOperand = ''
    this.previousOperand = ''
    his.operation = undefined
  }

  delete() {

  }

  appendNumber(number) {
   this.currentOperand = number
  }

  chooseOperation(operation) {

  }

  compute() {

  }

  updateDisplay() {
    this.currentOperandTextElement.innerText = this.currentOperand;
  }
}



const numberButtons = document.querySelectorAll('[data-number]')
const operationButtons = document.querySelectorAll('[data-operation]')
const equalsButton = document.querySelector('[data-equals]')
const deleteButton = document.querySelector('[data-delete]')
const allclearButton = document.querySelector('[data-all-clear]')
const previousOperandTextElement = document.querySelector('[data-previous-operand]')
const currentOperandTextElement = document.querySelector('[data-current-operand]')
    
// error was here
const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)



numberButtons.forEach(button => {
  button.addEventListener('click', () => {
    calculator.appendNumber(button.innerText)
    calculator.updateDisplay()
  })
})

PS always name your classes with upper case first letter, that is the convention PS 始终以大写首字母命名您的课程,这是惯例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Uncaught SyntaxError: Identifier '[x]' has been declared - 为什么? - Uncaught SyntaxError: Identifier '[x]' has already been declared - why? 未捕获的语法错误:标识符已被声明 - Uncaught SyntaxError: Identifier has already been declared 未捕获的 SyntaxError:标识符 '$...' 已被声明 - Uncaught SyntaxError: Identifier '$…' has already been declared 为什么我下面的异步 function 得到错误代码 'Uncaught SyntaxError: Identifier 'message' has already been declared”? - Why does my asynchronous function below get the error code 'Uncaught SyntaxError: Identifier 'message' has already been declared"? 为什么javascript在控制台中显示“未捕获的SyntaxError:在创建javascript类对象后已经声明了标识符'Common'”? - Why does javascript shows “Uncaught SyntaxError: Identifier 'Common' has already been declared after making javascript class object” in console? 我的石头剪刀布脚本不起作用。 控制台日志:未捕获的 SyntaxError:已声明标识符“playerSelection” - My Script for Rock Paper Scissors isn't working. Console logs: Uncaught SyntaxError: Identifier 'playerSelection' has already been declared javascript-未捕获的语法错误:标识符 * 已被声明 - javascript- Uncaught SyntaxError: Identifier * has already been declared 未捕获的SyntaxError:标识符'baseUrl'已被声明 - Uncaught SyntaxError: Identifier 'baseUrl' has already been declared 未捕获的 SyntaxError:标识符“按钮”已被声明 - Uncaught SyntaxError: Identifier 'Buttons' has already been declared 未捕获的语法错误:标识符“总计”已被声明 - Uncaught SyntaxError: Identifier 'total' has already been declared
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM