I've started to use Javascript a lot more, and as a result I am writing things complex enough that organization is becoming a concern. However, this question applies to any language that allows you to nest functions. Essentially, when should you use an anonymous function over a named global or inner function?
At first I thought it was the coolest feature ever, but I think I am going overboard. Here's an example I wrote recently, ommiting all the variable delcarations and conditionals so that you can see the structure.
function printStream() {
return fold(function (elem, acc) {
...
var comments = (function () {
return fold(function (comment, out) {
...
return out + ...;
}, '', elem.comments);
return acc + ... + comments;
}, '', data.stream);
}
I realized though (I think) there's some kind of beauty in being so compact, it is probably isn't a good idea to do this in the same way you wouldn't want a ton of code in a double for loop.