Javascript计算器不工作-错误消息为';类别';保留
我是Javascript新手,我发现很难创建一个工作的计算器,每当我点击计算器按钮时,我都没有收到任何响应,我也会收到错误消息“error:Parsing error:the keyword'class'is reserved class calculator”“如果你能帮我这样的新手,我将不胜感激。”。{ JSLint(2)Javascript计算器不工作-错误消息为';类别';保留,javascript,calculator,Javascript,Calculator,我是Javascript新手,我发现很难创建一个工作的计算器,每当我点击计算器按钮时,我都没有收到任何响应,我也会收到错误消息“error:Parsing error:the keyword'class'is reserved class calculator”“如果你能帮我这样的新手,我将不胜感激。”。{ JSLint(2) 要解决您的问题,请建立一个简单而简短的示例来说明它,而不是完整的150行代码…如果您真的希望得到答案,您从哪里得到这个错误?JSLint还是您的浏览器?哪个浏览器?抱歉,
要解决您的问题,请建立一个简单而简短的示例来说明它,而不是完整的150行代码…如果您真的希望得到答案,您从哪里得到这个错误?JSLint还是您的浏览器?哪个浏览器?抱歉,代码太长了,这是我的第一篇文章…错误在代码的开头,我的浏览器是Chrome。
class Calculator {
constructor(previousOperandTextElement, currentOperandTextElement) {
this.previousOperandTextElement = previousOperandTextElement
this.currentOperandTextElement = currentOperandTextElement
this.clear()
}
clear() {
this.currentOperand = ''
this.previousOperand = ''
this.operation = undefined
}
delete() {
this.currentOperand = this.currentOperand.toString().slice(0, -1)
}
appendNumber(number) {
if (number === '.' && this.currentOperand.includes('.')) return
this.currentOperand = this.currentOperand.toString() + number.toString()
}
chooseOperation(operation) {
if (this.currentOperand === '') return
if (this.previousOperand !== '') {
this.compute()
}
this.operation = operation
this.previousOperand = this.currentOperand
this.currentOperand = ''
}
compute() {
let computation
const prev = parseFloat(this.previousOperand)
const current = parseFloat(this.currentOperand)
if (isNaN(prev) || isNaN(current)) return
switch (this.operation) {
case '+':
computation = prev + current
break
case '-':
computation = prev - current
break
case '*':
computation = prev * current
break
case '÷':
computation = prev / current
break
default:
return
}
this.currentOperand = computation
this.operation = undefined
this.previousOperand = ''
}
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.innerText = this.getDisplayNumber(this.currentOperand)
if (this.operation != null) {
this.previousOperandTextElement.innerText =
$ {
this.getDisplayNumber(this.previousOperand)
}
$ {
this.operation
}
} else {
this.previousOperandTextElement.innerText = ''
}
}
}
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()
})
})
operationButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.chooseOperation(button.innerText)
calculator.updateDisplay()
})
})
equalsButton.addEventListener('click', button => {
calculator.compute()
calculator.updateDisplay()
})
allClearButton.addEventListener('click', button => {
calculator.clear()
calculator.updateDisplay()
})
deleteButton.addEventListener('click', button => {
calculator.delete()
calculator.updateDisplay()
})