function Box(age) { this.name = 'ss'; this.age = age; this.flag = true; return this;} //定義一個(gè)構(gòu)造函數(shù)var box1 = new Box(10); // new出一個(gè)實(shí)例setTimeout(function () { box1.flag = false; console.log(box1.flag);}, 5000); //五秒鐘之后把 實(shí)例box1里面的flag變?yōu)閒alse.var inter = setInterval(function () { if (box1) { console.log(box1); if (!box1.flag) { box1 = null; var box1 = new Box(20); } } else { console.log('cleared Interval as box1 is null now'); clearInterval(inter); }}, 1000); //每一秒種先控制臺(tái)打印出box1, 如果flag為false, 那么就銷毀box1,然后再new出一個(gè)box1. 結(jié)果是直接輸出box1是null.cleared Interval as box1 is null nowfalse請(qǐng)問(wèn)是不是由于var會(huì)優(yōu)先聲明局部變量. 導(dǎo)致聲明后直接box = null. 然后就輸出 else里面的內(nèi)容?
不知道是不是變量提升的問(wèn)題
桃花長(zhǎng)相依
2018-11-20 17:31:20