Anonymous function vs. separate named function for initialization in jquery
- by Martin N.
We just had some controversial discussion and I would like to see your opinions on the issue:
Let's say we have some code that is used to initialize things when a page is loaded and it looks like this:
function initStuff() { ...}
...
$(document).ready(initStuff);
The initStuff function is only called from the third line of the snippet. Never again.
Now I would say: Usually people put this into an anonymous callback like that:
$(document).ready(function() {
//Body of initStuff
});
because having the function in a dedicated location in the code is not really helping with readability, because with the call on ready() makes it obvious, that this code is initialization stuff.
Would you agree or disagree with that decision? And why?
Thank you very much for your opinion!