Because you forgot the semicolon after the first function expression:
(function () {
document.write("bar");
});
Otherwise the second "grouping operator" is interpreted as a function call. So this:
(function a() {
...
})
(function b() {
...
}());
is basically the same as:
function b() {
...
}
(function a() {
...
})(b());
Reordering makes it a bit easier to see. Remember that white space characters have no meaning in JavaScript and are ignored.