Javascript的自執行函數

  自執行函數其實也就是「當即執行的函數」,它有四個特色:提升性能、利於壓縮、避免衝突、依賴加載;函數

一、減小做用域查找性能

JS代碼:spa

1 // Anonymous function that has three arguments  
2 function(window, document, $) {  
3   
4   // You can now reference the window, document, and jQuery objects in a local scope  
5   
6 }(window, document, window.jQuery); // The global window, document, and jQuery objects are passed into the anonymous function 

也就是將做用域放到自執行函數的做用域中,Javascript解釋器首先會如今自執行函數的做用域內查找,其次再去全局查找。code

相關文章
相關標籤/搜索