1.
functionsay667()
functionsay667()varsayAlert=function(){alert(num);}
num ;returnsayAlert;
}
varsayAlert=say667();
sayAlert();
程式碼如下:
functionsetUpSomebals(>
functionsetUpSomebals(3)> varnum=666;
gAlertNumber=function(){alert(num);}gIncreaseNumber=function(){num ;}
gSetNumber=function(x){num=x;}
}
}
setUpSomeGlobals();//為三個全域變數賦值
gAlertNumber();//666
gIncreaseNumber();
gAlertNumber();//667gSetNumber(12);
gAlertNumber();//12
複製程式碼
程式碼如下:
functiontionp. >varresult=[];
for(vari=0;i
variteml='item' list[i];
result.push(function(){alert( item '' list[i]);});}returnresult;
}
functiontestList(){
varfnlist=buildList([1,2,3]);
for(varj=0;jfnlist[j]() ;
}}
程式碼如下:
functiontionals> 程式碼如下:
functiontionalsays() varsayAlert=function(){alert(alice);}
varalice='HelloAlice';
returnsayAlert;
}
varhelloAlice=sayAlice();
helloAlice();//HelloAlice
複製程式碼
程式碼如下:
functionRef.
varnum=someNum;
varanArray=[1,2,3];
varref=someRef;
returnfunction(x){num =x;
anArray.push(num );
alert('num:' num 'nanArray' anArray.toString() 'nref.someVar' ref.someVar);
}
}
closure1=newClosure(40,{someVar:'closure1'});
closure2=newClosure(1000,{someVar:'closure2'});
closure1(5);closure2(-10);