只有函數的表達式可以立即調用:在JavaScript中,爲什麼我不能立即調用函數聲明?
(function() {
var x = "Hello!!"; // I will invoke myself
})();
但不是函數聲明?這是因爲函數聲明被掛起並且已經立即執行?
編輯:資源我引用
http://benalman.com/news/2010/11/immediately-invoked-function-expression/
http://markdalgleish.com/presentations/gettingclosure/
函數x(){}與var x = function(){}相同,並且顯式變量「returns」void而不是賦值,這就是爲什麼你不能說alert (var x = 1),但你可以說alert(x = 1);對於函數來說是一樣的 – dandavis