我一直看到这样的代码:
(function(){
//code here
})();
Run Code Online (Sandbox Code Playgroud)
这段代码是如何工作的?哪个函数接收哪些参数?
(function(factory){
//code here
}(function($){
//other code here
}));
Run Code Online (Sandbox Code Playgroud)
function($){
//other code here
}
Run Code Online (Sandbox Code Playgroud)
该块作为参数传递给外部 IIFE。像这样写可能更清楚:
var factoryDef = function($) { ... };
(function(factory) {
// this is the outer IIFE
var someVar = {};
// you can call:
factory(someVar);
// the previous line calls factoryDef with param someVar
}(factoryDef));
Run Code Online (Sandbox Code Playgroud)
所以factory(someVar)是一样的factoryDef({}); 后者只是用(即.)的值调用的factory(即函数factoryDef)的值。someVar{}
那有意义吗?