function Box(age) { this.name = 'ss'; this.age = age; this.flag = true; return this;} //定義一個構造函數var box1 = new Box(10); // new出一個實例setTimeout(function () { box1.flag = false; console.log(box1.flag);}, 5000); //五秒鐘之后把 實例box1里面的flag變為false.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); //每一秒種先控制臺打印出box1, 如果flag為false, 那么就銷毀box1,然后再new出一個box1. 結果是直接輸出box1是null.cleared Interval as box1 is null nowfalse請問是不是由于var會優先聲明局部變量. 導致聲明后直接box = null. 然后就輸出 else里面的內容?
不知道是不是變量提升的問題
桃花長相依
2018-11-20 17:31:20