我正在阅读关于关闭的一些帖子,并在任何地方看到这个帖子,但没有清楚的解释它是如何工作的 - 每次我只是被告知使用它...:
// Create a new anonymous function, to use as a wrappe(function(){ // The variable that would, normally, be global var msg = "Thanks for visiting!"; // Binding a new function to a global object window.onunload = function(){ // Which uses the 'hidden' variable alert( msg ); };// Close off the anonymous function and execute it})();好吧,我看到我们将创建新的匿名函数,然后执行它。所以之后,这个简单的代码应该工作(和它):
(function (msg){alert(msg)})('SO');我的问题是这里发生了什么样的魔法?我以为,当我写道:
(function (msg){alert(msg)})那么就会创建一个新的未命名的函数,如函数“”(msg)...
但那为什么不工作?
(function (msg){alert(msg)});('SO');为什么需要在同一行?
你能指点我一些帖子还是给我一个解释?
相似问题