I'm learning node.js, and have noticed that pretty much all callbacks are inlined as anonymous callbacks into the function. Is there a specific reason behind doing things this way?
I think using a named callback, and defining it as a local function has 2 advantages: 1. it's cleaner, and doesn't turn the function into one giant block of code 2. given an appropriate name, it acts as documentation - describing what the callback is supposed to do
When using named functions in the global scope as callbacks, the scope where the function is named may cause the function to persist in memory and prevents it from being garbage collected. This is one of the many ways to cause memory leaks in your application. Anonymous functions on the other hand, are marked for GC immediately after their execution is over, and anything not returned (could be a closure as well) will be auto marked for garbage collection.
Consider a fairly complex jQuery plugin. Before generating and returning the actual object that is the subject of the plugin, it may have to create dozens of variables that hold temporary state data. If this were not done in an IIFE (Immediately invoked function expression: an anonymous function that is immediately executed), these variable would "leak" into the global scope. Data in JavaScript will remain in memory as long as there is any one variable or closure still referencing it. Since these variables have "leaked" to the global scope, they will remain in memory until that tab/window is closed. When defined inside an IIFE, the variables defined are stuck in the local anonymous function's scope. Thus, when the function has completed execution, the variables are "gone" and their data no longer has any references. The JS Engine's garbage collector notices that this specific data in memory is not being referenced anywhere anymore, and marks it for deletion, freeing up the occupied memory for other data.
So if you named your functions and you're calling them only once, it's possible they are needlessly taking up memory. Making them anonymous will reclaim the memory after execution, reducing your app's memory overhead.
This is essentially a description of how most dynamic languages work, and why they are more popular than static languages like C, where you have to keep track of every memory allocation you've made, and make sure you delete them when you no longer need them (an exercise in itself; deciding how long you'll need a particular data is not trivial always).