What is the difference between these two javascript constructs -


this question has answer here:

var myobject = (function(){    var value = 0;    return {     getvalue: function(){       return value;     }   }  }());    var myobject = (function(){    var value = 0;    return {     getvalue: function(){       return value;     }   }  })(); 

the execution seems return same object. i.e., myobject contains

  {{     getvalue: function(){       return value;     }   }} 

in both cases.

i know (function(){})() executes because (function(){}) expression returns function , trailing () executed function being returned.

but why execute (function(){}()) ? expecting syntax error here.

the phrases functionally identical, placement of () of matter of taste , i've see directions either in favour of other. prefer

(function() { ... }()); 

that form, creates function , executes inside of parenthesis.

(function() { ... })(); 

creates function inside of parenthesis , executes it.


Comments