close

在function中定義的function,內部的function可以存去外部function內定義的變數,這樣叫做Closure

example: 

  1. function test() {  
  2.     var _tid = null;  
  3.     var _count = 0;  
  4.     this.a = function () {  
  5.         _tid = setInterval(b,300);  
  6.     }  
  7.     function b() {  
  8.         if(_count>3) {  
  9.             clearInterval(_tid);  
  10.             _count = 0;  
  11.         } else {  
  12.             alert("round: " + _count);  
  13.             _count++;  
  14.         }  
  15.     }  
  16. }  
  17. var a = new test();  
  18. a.a();
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 benjamin16pp 的頭像
    benjamin16pp

    程式筆記

    benjamin16pp 發表在 痞客邦 留言(0) 人氣()