What is the cleanest way to use anonymous functions?
Posted
by Fletcher Moore
on Stack Overflow
See other posts from Stack Overflow
or by Fletcher Moore
Published on 2010-05-21T14:55:59Z
Indexed on
2010/05/21
15:01 UTC
Read the original article
Hit count: 294
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.
© Stack Overflow or respective owner