[英]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.