Javascript IIFE: Immediately Invoked Function Expression execution. how it runs? -


i trying understand how iife in js works.

i first ran this:

(function boo() {       var = 90;       console.log(i);     })(); 

and adding below

boo(); 

doesn't run.

q1: why? named iife not stored reference called later on?

when this

 var tee = function boo() {   var = 90;   console.log(i); };  tee();  boo(); 

it runs

but when this

 var tee = function boo() {   var = 90;   console.log(i); };  boo();  tee(); 

it doesn't run.

q2: why?

q1: why? named iife not stored reference called later on?

basically yes. name of function expression not become symbol in enclosing scope. e.g. if have

var foo = function bar() {   // `bar` defined here, `foo === bar` }; // `foo` defined here 

then can access foo. bar accessibly within function (and refers function itself). see named function expressions demystified more info.

q2

this has nothing iife. both of them broken.

the reason why don't see output in second example because trying access boo (which not exist) before calling tee (which exist).

javascript stops executing code when throws error, hence tee never called.


Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -