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

相关文章
相关标签/搜索