You-Dont-Know-JS icon indicating copy to clipboard operation
You-Dont-Know-JS copied to clipboard

Troubled Snippet, Scope & Closures Chapter 4

Open madmadi opened this issue 8 years ago • 5 comments

The last snippet code in Scope & Closures, Chapter 4 in Function First section seems to be incorrect. It didn't really work because foo is actually undefined, I test that in different environments but maybe I'm wrong but anyway it was confusing for me.

madmadi avatar Mar 19 '18 23:03 madmadi

Are you testing in a console or in a real js program (not node)?

getify avatar Mar 20 '18 01:03 getify

Yes, I've tested on standalone Firefox/Chrome console, an HTML file & Node env and I've got the same ReferenceError foo is not a function.

madmadi avatar Mar 20 '18 10:03 madmadi

I was just wondering if there is any update on this issue, I really want to get it clear on my mind.

itsdanielmatos avatar Apr 05 '18 13:04 itsdanielmatos

IIRC correctly, the spec calls for those to be hoisted, but I think browsers are allowed (in Appendix B) to violate this part of the spec, because of backwards compatibility. The whole issue of "functions in blocks" is confusing to me, because of this compat/breakage issue, and it changed (at least once) since the books were written. It's an open issue to try to re-address this topic in the second edition of the books. But my recommendation is going to be: avoid functions-in-blocks -- it's too murky to be worth it.

getify avatar Apr 05 '18 13:04 getify

the same issue for me

dragonishare avatar Oct 13 '19 10:10 dragonishare