1.
functionsay667(){
varnum=666;
varsayAlert=function(){alert(num);}
num ;
returnsayAlert;
}
varsayAlert=say667();
sayAlert();
2.
functionsetUpSomeGlobals(){
varnum=666;
gAlertNumber=함수(){alert(숫자);}
gIncreaseNumber=함수(){숫자 ;}
gSetNumber=함수(x){num=x;}
}
setUpSomeGlobals();//세 개의 전역 변수에 값 할당
gAlertNumber();//666
gIncreaseNumber();
gAlertNumber();//667
gSetNumber(12 );
gAlertNumber();//12
3.
functionbuildList(list) {
varresult=[];
for(vari=0;i
variiteml='item' list[i];
result.push(function() {alert( 항목 '' 목록[i]);});
}
returnresult;
}
functiontestList(){
varfnlist=buildList([1,2,3]);
for(varj=0;jfnlist[j]() ;
}
}
4.
functionsayAlice(){
varsayAlert=function(){alert(alice);}
varalice='HelloAlice';
returnsayAlert;
}
varhelloAlice=sayAlice();
helloAlice();//HelloAlice
5.
functionnewClosure(someNum, someRef){
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'});
클로저1(5);
클로저2(-10);