Btw, I stated in previous post - #6 - that Code_1 survives the minifier... I just checked and it does not! - It fails with the same issue, because - again - the single variable definition of a moves into the then path and if the else path is executed, overwrite of global a still happens... ouch:
The whole thing comes form JS var behavior introduced by the compile into byte code / the way byte code is generated: Any encountered var in a var-scoped block (function) moves the declaration IN THE BYTE CODE 'up' to be processed before the first execution statement... See Variable Hoisting - https://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/. The regular minifier is based on that behavior, takes advantage of it, and sees no issue with it. After all minification is the goal... compiler/byte code generation will take care of proper sequencing.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Btw, I stated in previous post - #6 - that Code_1 survives the minifier... I just checked and it does not! - It fails with the same issue, because - again - the single variable definition of a moves into the then path and if the else path is executed, overwrite of global a still happens... ouch:
Code_1 minified:
A small change though makes a big difference and makes the variable definition to survive the minifier and also already safes 11 bytes....
Code_1.1:
Code_1.1 minified:
Very interesting.
The whole thing comes form JS var behavior introduced by the compile into byte code / the way byte code is generated: Any encountered var in a var-scoped block (function) moves the declaration IN THE BYTE CODE 'up' to be processed before the first execution statement... See Variable Hoisting - https://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/. The regular minifier is based on that behavior, takes advantage of it, and sees no issue with it. After all minification is the goal... compiler/byte code generation will take care of proper sequencing.
Espruino does no hoisting... it sticks to the sequence by its very own and unique nature... (see https://www.espruino.com/Features#general).