The difference between IIFE and the challenge

Is there a difference between:

(function(){ }).call(this); 

and

 (function(){ })(); 

or

 var MODULE = {}; (function(){ this.hello = 'world' }).call(MODULE); 

and

 var MODULE = {}; (function(m){ m.hello = 'world' })(MODULE); 

I often see the first case in compiled javascript. They both create a scope and do a good job of expanding names.

Is there any difference or is it just a matter of taste.

Edit: And why will compiled javascript use a call through IIFE?

+8
javascript scope namespaces
source share
1 answer
 (function(){ }).call(this); 

calls an anonymous function in which this inside the function will point to the object indicated by this when called.

 (function(){ })(); 

calls an anonymous function, where this inside the function will point to a global object (or undefined in strict mode)

Demo: Fiddle

+7
source share

All Articles