Javascript的自执行函数

  自执行函数其实也就是“立即执行的函数”,它有四个特点:提高性能、利于压缩、避免冲突、依赖加载;

1、减少作用域查找

JS代码:

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解释器首先会现在自执行函数的作用域内查找,其次再去全局查找。

原文地址:https://www.cnblogs.com/roberter-chen/p/5983602.html