I ve been looking at a lot of JavaScript code lately and I ve seen two different ways of using assigning "public" properties of IIFE s.
The first is to create a variable and assign that variable to a property inside of the IIFE like so:
var public1;
(function(){
var foo= "Foo", bar= "Bar";
public1= {
getFoo: function(){
return foo;
}
};
}());
The second way I see is returning an object from the IIFE like so:
var public2 = (function(){
var foo2= "Foo2", bar2= "Bar2";
return {
getBar: function(){
return bar2;
}
};
}());
Is there a fundamental difference between these two ways or is it just a matter of preference? I ve also created a fiddle so you can run or update the code if you d like: http://jsfiddle.net/bittersweetryan/gnh79/3/