You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • 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:

    move: function(b){
      if(l.selectEdit){
        var a=l.selectEdit;
        a.value-=(b||1)*(a.step||1);
        void 0!==a.min&&a.value<a.min&&(a.value=a.min­);
        void 0!==a.max&&a.value>a.max&&(a.value=a.max­);
        if(a.onchange)a.onchange(a.value);
        l.draw(options.selected,options.selected­)}
      else a=options.selected,options.selected=(b+o­ptions.selected)%menuItems.length
         , 0>options.selected&&(options.selected+=m­enuItems.length)
         , l.draw(Math.min(a,options.selected),Math­.max(a,options.selected))
    };
    

    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:

    move: function(dir) {
          var item = l.selectEdit,a;
          if (item) {
            item.value -= (dir||1)*(item.step||1);
            if (item.min!==undefined && item.value<item.min) item.value = item.min;
            if (item.max!==undefined && item.value>item.max) item.value = item.max;
            if (item.onchange) item.onchange(item.value);
            l.draw(options.selected,options.selected­);
          } else {
            a=options.selected;
            options.selected = (dir+options.selected)%menuItems.length;­
            if (options.selected<0) options.selected += menuItems.length;
            l.draw(Math.min(a,options.selected), Math.max(a,options.selected));
          }
        };
    

    Code_1.1 minified:

    move: function(b){
      var a=l.selectEdit;
      if(a){
        a.value-=(b||1)*(a.step||1);
        void 0!==a.min&&a.value<a.min&&(a.value=a.min­);
        void 0!==a.max&&a.value>a.max(a.value=a.max);­
        if(a.onchange)a.onchange(a.value);
        l.draw(options.selected,options.selected­)}
      else a=options.selected
         , options.selected(b+options.selected)%men­uItems.length
         , 0>options.selected&&(options.selected+=m­enuItems.length)
         , l.draw(Math.min(a,options.selected),Math­.max(a,options.selected))
    };
    

    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-j­avascript-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#genera­l).

About

Avatar for allObjects @allObjects started