JavaScript计算器-无法理解代码中的几行

JavaScript计算器-无法理解代码中的几行,javascript,Javascript,我是一名编码初学者,在关注YouTube频道WebDevSimplified的同时,我遇到了下面制作计算器的JS代码。在下面的代码中,我无法理解在没有定义的情况下如何使用currentOperator和previousOperator?它看起来像是currentOperator引用了currentOperatureTextElement和PreviousOperator引用了PreviousOperatureTextElement,但是它没有在代码中的任何地方定义。下面是GitHub repo的

我是一名编码初学者,在关注YouTube频道WebDevSimplified的同时,我遇到了下面制作计算器的JS代码。在下面的代码中,我无法理解在没有定义的情况下如何使用
currentOperator
previousOperator
?它看起来像是
currentOperator
引用了
currentOperatureTextElement
PreviousOperator
引用了
PreviousOperatureTextElement
,但是它没有在代码中的任何地方定义。下面是GitHub repo的链接

请大家帮我理解。非常感谢你

类计算器{
构造函数(PreviousOperationTextElement、CurrentOperationTextElement){
this.PreviousOperationTextElement=PreviousOperationTextElement
this.currentOperatureTextElement=currentOperatureTextElement
这个
}
清除(){
this.currentOperator=“”
this.previous操作数=“”
this.operation=未定义
}
删除(){
this.currentOperator=this.currentOperator.toString().slice(0,-1)
}
附录编号(编号){
if(number=='..&&this.currentOperator.includes('.')返回
this.currentOperator=this.currentOperator.toString()+number.toString()
}
选择操作(操作){
如果(this.currentOperator=='')返回
if(this.previousOperator!=''){
这个是compute()
}
此操作=操作
this.previous操作数=this.currentOperator
this.currentOperator=“”
}
计算(){
让计算
const prev=parseFloat(this.previous操作数)
const current=parseFloat(此.currentOperator)
if(isNaN(上一个)| isNaN(当前))返回
开关(此操作){
格“+”:
计算=上一次+当前
打破
案例'-':
计算=上一次-当前
打破
案例“*”:
计算=上一次*当前
打破
案例“÷”:
计算=上一次/当前
打破
违约:
返回
}
this.currentOperator=计算
this.operation=未定义
this.previous操作数=“”
}
getDisplayNumber(数字){
const stringNumber=number.toString()
常量integerDigits=parseFloat(stringNumber.split('.')[0])
常量小数位数=stringNumber.split('.')[1]
让整数显示
if(isNaN(整数数字)){
整数显示=“”
}否则{
integerDisplay=integerDigits.toLocaleString('en',{maximumFractionDigits:0})
}
if(小数位数!=null){
返回`${integerDisplay}.${decimalDigits}`
}否则{
返回整数显示
}
}
updateDisplay(){
this.currentOperationTextElement.innerText=
此.getDisplayNumber(此.CurrentOperator)
if(this.operation!=null){
this.PreviousOperationsTextElement.innerText=
`${this.getDisplayNumber(this.previousOperator)}${this.operation}`
}否则{
this.PreviousOperationrTextElement.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 previousOperationTextElement=document.querySelector(“[data previous Operator]”)
const currentOperatureTextElement=document.querySelector(“[数据当前操作数]”)
常量计算器=新的计算器(PreviousOperationsTextElement、CurrentOperationsTextElement)
numberButtons.forEach(按钮=>{
按钮。addEventListener('单击',()=>{
计算器.appendNumber(按钮.innerText)
calculator.updateDisplay()
})
})
操作按钮。forEach(按钮=>{
按钮。addEventListener('单击',()=>{
计算器.chooseOperation(按钮.innerText)
calculator.updateDisplay()
})
})
equalsButton.addEventListener('单击',按钮=>{
计算器
calculator.updateDisplay()
})
allClearButton.addEventListener('单击',按钮=>{
计算器清除()
calculator.updateDisplay()
})
deleteButton.addEventListener('单击',按钮=>{
计算器。删除()
calculator.updateDisplay()
})

当前操作数
以前的操作数
在类
计算器
上动态创建为字段。由于它们都未在构造函数中定义,因此它们的初始值为
未定义


例如,由于运算是通过
appendNumber
在计算器上执行的,
this.currentOperator
被分配了一个值,以后可以由
chooseOperation()
compute()
使用,我不明白您的困惑所在。
this.currentOperator
this.previousOperator
有几个地方是赋值的。它们在构造函数的参数中定义(到处)。我想这是学习在代码顶部和可能的变量范围声明变量的一个练习。它们被定义为中间的DOM节点:
const-previousoperantextElement=document.querySelector(“[data-previous-operand]”)和
const-currentoperantextElement=document.querySelector('[数据当前操作数]”)
老实说,这看起来更像是一个家庭作业。不要欺骗自己。为问这个愚蠢的问题向所有人道歉