javascript知識點雜記

來源:互聯網
上載者:User

標籤:++   聲明   script   error:   ror   外部變數   func   span   傳遞   

1 for(var i = 0; i < 10; i++) {2     setTimeout(function() {3     console.log(i);  //輸出10個10,因為setTimeout方法是非同步,當執行到setTimeout時,for迴圈已經執行完畢,i變數值為104     }, 1000);5 }

 

1 for (var i = 0; i < 10; i++) {2     (function(a) {3     // 變數 i 的值在傳遞到這個範圍時被複製給了 a,4     // 因此這個值就不會隨外部變數而變化了5     setTimeout(function() {6         console.log(a);  //輸出0 1 2 3 4 5 6 7 8 97         }, 1000);8     })(i); // 我們在這裡傳入參數來"閉包"變數9 }

 

 1 //聲明式函數定義語句屬於頂級範圍範圍,函數調用語句可寫在函式宣告語句之前: 2 add(1, 2);//3 3 function add(m, n) { 4     alert(m + n); 5 } 6  7  8 //函數運算式定義的函數在定義之前無法調用: 9 //TypeError: add is not a function10 add(1, 2);11 var add = function(m, n) {12     alert(m + n);13 };

 

javascript知識點雜記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.