3 ms·
Because once you allow statements at the top level alongside declarations, which statement should be run first? main() makes that clear. In JavaScript it depen
by spankalee 13y ago
Because once you allow statements at the top level alongside declarations, which statement should be run first? main() makes that clear.
In JavaScript it depends on the order of script tags and their attributes. It's really hard to determine what code is actually run at application startup, and what the initial state is.
Making the top-level only declarations means that the programmer knows what code runs first (the first line in main()) and the VM knows that the order of evaluating the declarations doesn't matter. It can evaluate them all and cache the result so that subsequent runs are fast, it can execute main() before other declarations and evaluate them on an as-needed basis, or something in between.
- nailer 13y agoConvention? I guess it does comedown to function hoisting in the end, so the expectation is the body of code after the functions are defined / function variables created. I think a lot of people prefer that convention over boilerplate code, and the tide of language development seems to be heading in that direction. OT: script tag soup is, for first party stuff anyway, avoided with a single script for a module loader these days.