我在cacheDom函数中遇到$display
,但是当我尝试在updateResult函数this.$display.text(temp).apply(this);
中使用它时它不起作用。我认为这与范围规则有关,我肯定错过了一些东西。任何帮助将不胜感激。
$(document).ready(function() {
(function() {
var current = "", result = 0;
var calculator = {
init: function() {
this.cacheDom();
this.render();
this.bindEvents();
},
cacheDom: function() {
this.$button = $(".button p, .zero p");
this.$display = $("#display p");
},
bindEvents: function() {
this.$button.on("click", this.updateResult);
},
updateResult: function() {
var temp = $(this).text();
if (parseInt(temp) > 0 == false) {
if (temp == "÷") {
current += "/";
this.$display.text(temp).apply(this);
} else if (temp == "×") {
current += "*";
$("#display p").text(temp);
} else if (temp == "-") {
current += temp;
$("#display p").text(temp);
} else if (temp == "+") {
current += temp;
$("#display p").text(temp);
} else if (temp == "=") {
current = eval(current);
$("#display p").text(current);
} else if (temp == "C") {
current = "";
temp = "";
result = 0;
$("#display p").text(result);
} else if (temp == "%") {
current = current / 100;
$("#display p").text(current);
} else if (temp == "√") {
$("#display p").text(Math.sqrt(parseInt(current[current.length - 1])));
var sqrt = Math.sqrt(parseInt(current[current.length - 1]));
current = current.substring(0, current.length - 1)
current += sqrt;
}
} else {
$("#display p").text(temp);
current += temp;
}
console.log(current);
},
render: function() {
},
};
calculator.init();
})()
});