代碼如下:jsvardiv=$('#appendHere');$('#clickMe').on('click',function(){varthat=this;div.append(checkForWindow(this));setTimeout(function(){div.append(checkForWindow(this));div.append('thatisthe'+that.tagName+'');},300);});functioncheckForWindow(elm){if(elminstanceofWindow){return'thisistheWindow';}elseif(elm.tagName){return('thisisthe'+elm.tagName+'');}else{return('thisis'+elm+'');}}輸出結(jié)果:(另見:http://jsfiddle.net/dposin/okjr81ev/light/)thisistheBUTTONthisistheWindowthatistheBUTTON問(wèn)題來(lái)了,setTimeout中的checkForWindow(this)為什么沒有形成閉包,而that又形成閉包了呢?這樣寫就能形成閉包?本來(lái)我以為是要這樣寫的:jsfor(vari=0;i
setTimeout閉包疑問(wèn)
眼眸繁星
2019-04-13 08:36:42