IIFE calls the evaluation function

I came across strange behavior related to operations invoked right away.

var foo = function(){ alert('foo'); } (function(){ })(); 

http://jsfiddle.net/tmh8hpum/

Run the above code and notice how the function foo is evaluated, but ..

 var foo = function(){ alert('foo'); }; (function(){ })(); 

http://jsfiddle.net/fwcst8w6/1/

Add a terminator to the function and it is no longer called.

I expected IIFE to only evaluate code inside its own function body. Why does IIFE cause a function to endlessly be evaluated directly above it?

Even a stranger, he only seems to function directly above him; run this script and notice how only bar () is evaluated.

+5
source share

Source: https://habr.com/ru/post/1210188/


All Articles