What is the difference between these two Module pattern forms in Javascript? [duplicate]

This question already has an answer here:

I’ve noticed that in different code samples on the web that there are two different closure forms — but I haven’t been able to find anything that identifies the difference (if any) between the two.

var module = (function() { ... })()

and

var module = (function() { ... }())

They both seem functionally equivalent in the sample below, but I seem to recall that at one time, the distinction was important (code worked with one form but not the other).

var sdk =(function(){
var privacy='illusion';
return (
    { 
        getPrivacy: function(){return privacy;}
    });
})()

console.log(sdk.getPrivacy());

seems to be identical to that the code fragment where the next-to-last line is replaced with }())

The same also seems to be true if I pass in a Module object to associate the public properties/methods to:

var sdk =(function($){
  var privacy='illusion';
  $.getPrivacy = function() { return privacy;}
  return($);
})(sdk=sdk||{})

console.log(sdk.getPrivacy());

or its counterpart form ending in }(sdk=sdk||{}))


Source: syntax

Leave a Reply