如果需要將作用域保留在嵌套函數中,通常會在嵌套函數中創建一個如下所示的變量:var that = this;
和參考that
。第三學位及以後的會議名稱是什麼?用於第三個(或更多)嵌套作用域的JS作用域約定
我也遇到過一些使用代理的情況,但從來沒有必須在JQuery中傳遞嵌套代理。有什麼特殊的嵌套proxy
小號
正常範圍問題在骨幹上來了,有一個潛在的(我們仍然可以通過它的工作)不得不去另一個層面下來:
keyPressed: function(keyEvent) {
if(case1 && case2 && case3) {
if(anotherCase) {
.....
}
else if(yetAnotherCase) {
....
var that = this;
window.waitIntervalID = window.setInterval(function() {
if(that.waitCount == 2) {
that.isWaiting = false;
that.waitCount = 0;
that.mainCounter = 0;
that.isRecording = true;
for(var i = 0; i < that.signature; i++) {
that.beatArray[i] = 0;
}
window.tapIntervalID = window.setInterval(function() {
that.count = that.mainCounter + 1;
that.mainCounterTime = new Date().getTime();
that.mainCounter = (that.mainCounter + 1) % that.signature;
}, that.average);
that.isTapping = false;
that.countIn = 1;
var bpm = 1000/that.average * 60;
that.set('tempo', bpm);
window.clearInterval(waitIntervalID);
}
that.waitCount++;
}, this.average);
this.countIn++;
}
}
},
代理範圍:
// add click handler to this beat
$('#beat'+this.model.cid).click($.proxy(this.toggle, this));
return this;
貴國是否能說明爲什麼/如何你想改變的變量名的例子在不同的範圍? – 2013-04-28 17:19:43
@MattKantor發佈 – 2013-04-29 02:08:39