Я строю калькулятор, используя класс.Сейчас я тестирую, чтобы увидеть, когда я нажму цифровую кнопку, что она будет отображаться.Это не ошибка.Uncaught TypeError: Невозможно установить свойство 'innerText' из неопределенного.Теперь, когда я удаляю ключевое слово this в методе updateDisplay, все работает нормально?Как будто ключевое слово this указывает на глобальный объект?Что здесь происходит?
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]');
class Calculator {
contructor(previousOperandTextElement, currentOperandTextElement) {
this.previousOperandTextElement = 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;
/* the code above throws an error, below works fine, so without the
this keyword? */
currentOperandTextElement.innerText = this.currentOperand;
}
}
const calculator = new Calculator(previousOperandTextElement,
currentOperandTextElement);
numberButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.appendNumber(button.innerText)
calculator.updateDisplay();
})
})