3 ms·
Why and how could function hoisting be useful? (Genuine question, not an snarky comment) Edit: Huh! I thought you meant "variable hoisting", which is the most
by pooriaazimi 14y ago
Why and how could function hoisting be useful?
(Genuine question, not an snarky comment)
Edit: Huh! I thought you meant "variable hoisting", which is the most evil language feature that's ever bitten me!
This is an introduction to "function hoisting": http://elegantcode.com/2011/03/24/basic-javascript-part-12-function-hoisting/ http://elegantcode.com/2011/03/24/basic-javascript-part-12-f... - Very very interesting.
- jarek-foksa 14y agoI always felt that function hoisting is harming code readability. I prefer reading the code from top to bottom, I don't want to stuble upon functions that were not declared before. If I recall correctly the main reason why this feature is not supported by CoffeeScript is because of some awkward IE bug though.
- jashkenas 14y agoThe main reason is what you say -- it harms readability. Much better to have functions behave like any other value, like any other variable -- and only be usable after they've been created.
- tripzilch 14y agoI agree, although I also see some usefulness: If your inner function names are sufficiently descriptive, you can write the high-level behaviour of the outer function at the top, first to be read, to get a quick overview of what it does. Then declare the inner functions below that (this also requires that the inner functions must not have surprising side-effects). I've never seen or tried out code like that in practice, but it seems like it could work.