You are reading a single comment by @DaveNI and its replies. Click here to read the full conversation.
  • Ok, quick update - I was wrong. As an example:

    var b = (function(a) {
      // code I don't care about
      for (var i=0;i<10;i++);
      // ...........
      return function() { console.log("Hello "+a); }
    })();
    
    trace(); // <--- this is handy, it outputs what is in memory in a human-readable (ish) format
    
    #1[r2,l1] Object { 
      #2[r1,l2] Name String [1 blocks] ">"    #3[r1,l2] Object { 
         // we don't care about stuff in here
        } 
      #22[r1,l2] Name String [1 blocks] "b"    #40[r1,l1] Function { 
          #41[r1,l2] Name String [1 blocks] ">cod"        #38[r1,l1] String [2 blocks] "{ console.log(\"Hello \"+a); }"  
          #42[r1,l2] Name String [1 blocks] ">sco"        #34[r1,l1] Function { 
              #35[r1,l2] Name Param             undefined 
              #36[r1,l2] Name String [1 blocks] "return"            undefined 
              #37[r1,l2] Name String [1 blocks] "i"= int 10
            } 
        } 
    }
    

    So the function that has been executed gets completely removed (even though its execution context remains). This means that it's best to do the following (which will allow the minifier to work its magic):

    var onInit = (function() {
          var aaa, bbb, ccc;
          function one(){
          }
          function two(){
          }
          function three(){
          }    
          return function() {
            // stuff you actually want to execute on initialisation
            one();
          };
    })();
    

    That doesn't work so well for modules though I'm afraid, because the functions that are defined still need the 'module scope', which references the function that defined them.

  • Thanks for such a quick, comprehensive answer - I'll try some of this out later.

    I actually quite enjoy having limited ram- it forces me to think about every function in detail and can result in a much better end product. With unlimited resources code can become bloated really quickly.

About

Avatar for DaveNI @DaveNI started