Anonymous function vs. separate named function for initialization in jquery
Posted
by
Martin N.
on Programmers
See other posts from Programmers
or by Martin N.
Published on 2012-06-11T12:19:30Z
Indexed on
2012/06/11
16:47 UTC
Read the original article
Hit count: 302
coding-style
|jQuery
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!
© Programmers or respective owner