我在 Javascript 中遇到了一个简单的问题,我想比较两个变量,但不知何故它无法正常工作。我什至检查了变量的类型是否相同。请帮忙。 具体来说,我收到数字警报,甚至收到当前计数器为 5 总计为 5 的警报,尽管我希望它输入“然后”。
function fun(idCount, finalCallback) {
this.idCount = parseInt(idCount);
var counter = 0;
var strings = {};
this.register = function(str, id) {
counter++;
strings[parseInt(id)] = str;
if(counter == this.idCount) {
alert("Going to override");
//blabla
} else {
alert("Current counter is: " + counter + " Total " + idCount + ". Types: " + typeof counter + " and " + typeof idCount);
}
}
}
最佳答案
试试这个,
function fun(idCount, finalCallback) {
var self = this;
this.idCount = parseInt(idCount, 10);
var counter = 0;
var strings = {};
this.register = function(str, id) {
counter++;
strings[parseInt(id, 10)] = str;
if(counter == self.idCount) {
alert("Going to override");
//blabla
} else {
alert("Current counter is: " + counter + " Total " + idCount + ". Types: " + typeof counter + " and " + typeof idCount);
}
}
}
关于javascript - 变量比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21940278/