I have known the key of closure is about scope. But I don't know the specific execution order of it.
A classical code from 《Professional JavaScript for Web Developers 3rd Edition》about closures.
function createFunctions(){
 var result=new Array();
 alert('haha');
 for(var i=0;i<10;i++){
  result[i]=function(){
   alert(i);
   return i;
  };
 }
 return result;
}
var a=createFunctions();To my surprise, 'haha' was alerted while none of 'i' were alerted.I was just assigning the function to variable a.Why the statement "alert('haha')" was alerted while the cycle wasn' t executed?
when I add the below code,
 var a=createFunctions();
    alert(a[2]);why it alerted like this
function (){
       return i;
      }not like
function (){
          return 2;
          }Furthermore when I add the below code, what will being the order of statements executed especially the statements in the cycle.
    var a=createFunctions();
       alert(a[2]()); 
     
    