You have got your code wrong. It should be self executing function like this:
(function(baz) {
var foo = true,
bar = false;
if (baz) bar = true;
return function() {}; // wut?
});
So you just forgot (
in front of it.
Now you probably will use it as an expression so it will become like this
var singleton = (function(baz) {
var foo = true,
bar = false;
if (baz) bar = true;
return function() {}; // wut?
});
Now your singleton variable will be a simple empty function.
If you put in
return function() {
return foo;
}
it becomes useful as this:
singleton()
<- returns true
And is very similar to module pattern where you just write:
return {
myFoo: foo
}
And so you can access it with singleton.myFoo
;
This is useful to implement public/private variables so you just return functions/variables you want to expose.