<script type="text/javascript"> //閉包(closure):內層函數能夠引用存在於包圍它的函數內的變量,即便外層函數的執行已經結束 //注意內層函數引用的外層函數內的變量是外層函數執行結束後的最終值 test(1); function test(a) { //外層函數 alert(a+' 外層函數開始執行'); setTimeout(function(){//內層函數 alert(a+' 雖然外層函數執行完畢,可是內層函數依然能夠引用外層函數內的變量'); },5000); alert(a+' 外層函數執行完畢'); a +=1; } alert('外層函數確實執行完畢');</script><script type="text/javascript"> //一個有趣的例子 function add(b) { return function (c) { return b + c; } } var test = add(1); alert(test(1));</script>