»õ·Î¿î ±â´ÉÇÔ¼ö¸¦ ¸¸µé°í, ÂüÁ¶ÇÏ´Ù.
±¸¹® 1 : function functionName([param1][,param2][...,paramN]){JsCode}
±¸¹® 2 : functionName=new Function([param1][,param2][...,paramN]){JsCode}
- Àμö
-
- functionName : (±â´ÉÇÔ¼ö ¸íĪ)
- ÇʼöÀûÀÎ ¿ä¼Ò. »õ·Î ¸¸µé ±â´ÉÇÔ¼ö À̸§ÀÌ´Ù.
´Ü¾î 'function'Àº ¿¹¾à¾îÀ̹ǷΠfunctionÀ» ±â´ÉÇÔ¼ö(function)¸í(name)À¸·Î »ç¿ëÇÒ ¼ö ¾ø´Ù.
±×·¯¹Ç·Î ¼öÇàÇÏ´Â ±â´ÉÀ» Àß ³ªÅ¸³»´Â ´Ü¾î·Î Á¶ÇÕÇÏ¿© »ç¿ëÇÑ´Ù.
functionÀº »ç¿ëÀÚ°¡ ±â´ÉÇÔ¼ö¸¦ ¸¸µé ¶§ »ç¿ëÇÏ´Â ¹®ÀåÀÌ´Ù.
- param1,param2,...paramN : (±â´ÉÇÔ¼öÀÇ Àμö)
- ¼±ÅÃÀûÀÎ ¿ä¼Ò. ±â´ÉÇÔ¼ö¿¡ »ç¿ëÇÏ´Â Àμö ¸ñ·ÏÀÌ´Ù.
- JsCode : (±â´ÉÇÔ¼öÀÇ ¼öÇ๮ º»Ã¼)
- ¼±ÅÃÀûÀÎ ¿ä¼Ò. ±â´ÉÇÔ¼ö¸¦ È£ÃâÇÏ¸é ½ÇÇàµÇ´Â Javascript ÄÚµå ºí·ÏÀ» Æ÷ÇÔÇÑ ¹®ÀÚ¿ÀÌ´Ù.
- Ư¼º
-
Javascript¿¡¼ FunctionÀº °ªÀ» °®´Â ±âº»ÀûÀÎ µ¥ÀÌÅÍ Çü½ÄÀÌ´Ù.
function ¹®ÀåÀº ¼öÄ¡, ¹®ÀÚ¿, °³Ã¼µé, ¹ÝȯµÇ´Â °ªµéÀ» Æ÷ÇÔÇÏ´Â ÁöÁ¤µÈ ÆĶó¸ÞÅÍ(parameters)¸¦ »ç¿ëÇÏ¿© ±â´ÉÇÔ¼ö°¡ Çü¼º µÊÀ» ¼±¾ðÇÑ´Ù. Function °³Ã¼´Â °íÀ¯ÀÇ ¼Ó¼ºµé°ú ¸Þ¼µåµéÀ» °®´Â´Ù.
- ±¸¹®
-
- ±¸¹® 1Àº Javascript¿¡¼ »õ ±â´ÉÇÔ¼ö¸¦ ¸¸µå´Â Ç¥ÁØ ¹æ¹ýÀÌÀ¸·Î, ÀÌ Function °³Ã¼¸¦ »ç¿ëÇϸé Javascript¿¡¼ ÇÊ¿ä¿¡ µû¶ó Function °³Ã¼·Î º¯È¯ÇÏ´Â ±â´ÉÇÔ¼ö°ªÀ» ¸¸µç´Ù.
- ±¸¹® 2´Â ±â´ÉÇÔ¼ö °³Ã¼µéÀ» ¸í½ÃÀûÀ¸·Î ¸¸µé °æ¿ì »ç¿ëÇÏ´Â ´ëü Çü½ÄÀ̸ç, ±â´ÉÇÔ¼ö¸¦ È£ÃâÇϸé Javascript¿¡¼ ±¸¹® 2·Î ¸¸µç Function °³Ã¼¸¦ ±â´ÉÇÔ¼ö°ªÀ¸·Î º¯È¯ÇÑ´Ù.
- ¹Ýȯ°ª
-
±â´ÉÇÔ¼ö ³»ºÎ¿¡¼ ¼±¾ðÇÑ º¯¼ö´Â ±â´ÉÇÔ¼ö ¹Û¿¡¼´Â »ç¿ë ÇÒ ¼ö ¾ø´Ù. function() ±â´ÉÇÔ¼ö¿¡¼ ƯÁ¤ °ªÀ» ¹ÝȯÇϱâ À§Çؼ´Â return ¹®ÀåÀ» »ç¿ëÇÏ¿© ¹ÝȯÀ» ¸í½ÃÇÏ¿©¾ß ÇÑ´Ù.
- »ç¿ë
-
¾ðÁ¦³ª ÇÊ¿äÇÒ ¶§¿¡ ±â´ÉÇÔ¼ö Function °³Ã¼ ¸¦ È£ÃâÇÏ¿© ±× °ªÀ» »ç¿ë ÇÒ ¼ö ÀÖ´Ù.
Function ±¸¼ºÀÚ(constructor)¸¦ »ç¿ëÇÏ¿© ±â´ÉÇÔ¼ö¸¦ ¸¸µé¸é ¸Å¹ø °ËÁ¤À»ÇÏ°Ô µÇ¹Ç·Î È¿°úÀûÀÎ ¹æ¹ýÀÌ ¸øµÇ°í, ¹Ý¸é¿¡ function ¹®ÀåÀ» »ç¿ëÇÏ¿© ±¸¼ºÇϸé ÄÄÆÄÀϵǾî È¿°úÀûÀÌ µÈ´Ù. ±â´ÉÇÔ¼öÀÇ È£ÃâÀº ±¸¹® 1À̳ª ±¸¹® 2¿¡¼ °øÈ÷
functionName(necessaryParameters);
function ¹®ÀåÀ» »ç¿ëÇÑ ¿¹Á¦
±¸¹® 1
<SCRIPT>
function calcAverage(a,b,c,d){
return (a+b+c+d)/4;
}
document.write(calcAverage(1,9,2,3));
</SCRIPT>
Function constructor¸¦ »ç¿ëÇÑ ¿¹Á¦
±¸¹® 2
<SCRIPT>
var calcAverage=new Function('a','b','c','d','return((a+b+c+d)/4)');
document.write(calcAverage(1,9,2,3));
</SCRIPT>
Function constructor¸¦ »ç¿ëÇÑ ¿¹Á¦
¾ß·¡ ¿¹Á¦¿¡¼ »ý¼ºµÈ myCompany °³Ã¼´Â name, phone, total ¼Ó¼ºÀ» °®°í, addMembÀ̶ó´Â »õ·Î¿î Function °³Ã¼°¡ »ý¼ºµÇ°í, ÀÌ ±â´ÉÇÔ¼ö¸¦ È£ÃâÇÏ¸é¼ Àοø¼ö¸¦ °¡»êÇÒ ¼ö ÀÖµµ·Ï(¿¹Á¦¿¡¼´Â 5) µÇ¾î ÀÖ´Ù. °¡»êµÈ ÃÑ¿øÀÌ total ¼Ó¼º¿¡ ¹Ý¿µµÈ´Ù.
<SCRIPT>
myCompany={name:'Trio',phone:'123456789',total:25};
var addMemb=new Function('obj','x','obj.total+=x'); // Function constructor¸¦ »ç¿ë
addMemb(myCompany,5);
document.write(myCompany.name+', ÃÑ¿ø='+myCompany.total+', ÀüȹøÈ£='+myCompany.phone);
</SCRIPT>
addMemb´Â ±â´ÉÇÔ¼ö¸¦ ÂüÁ¶ÇÏ´Â ÀÏÁ¾ÀÇ º¯¼öÀÌ´Ù.
- ÁÖÀÇ
-
<SCRIPT>document.write(calcAverage)</SCRIPT>
Âü°í ; ±â´ÉÇÔ¼ö¸¦ È£ÃâÇÒ ¶§´Â ¹Ýµå½Ã °ýÈ£¿Í ÇʼöÀûÀÎ Àμö°¡ ÀÖ¾î¾ß ÇÑ´Ù. °ýÈ£ ¾øÀÌ ±â´ÉÇÔ¼ö¸¦ È£ÃâÇÏ¸é ±â´ÉÇÔ¼ö °á°ú ´ë½Å ±â´ÉÇÔ¼ö ÅؽºÆ®°¡ ¹ÝȯµÈ´Ù.(À °æ¿ì)
<SCRIPT>document.write(calcAverage())</SCRIPT>
<SCRIPT>document.write(calcAverage(1,2,3,4))</SCRIPT>
±â´ÉÇÔ¼ö°¡ Àμö°ªÀ» º¯°æ½ÃÅ°¸é, Àμö°¡ º°°³ÀÇ °³Ã¼°¡ ÀÌ´Ñ ÀÌ»ó ±× º¯°æÀÌ ±â´ÉÇÔ¼öÀ̳ª Àü¹ÝÀûÀ¸·Î ȤÀº ±× È£ÃâµÇ¾ú´ø ±â´ÉÇÔ¼ö¿¡ Àû¿ëµÇÁö ¾Ê´Â´Ù. º°°³ÀÇ °³Ã¼ÀÎ °æ¿ì¿¡´Â ±× ¼Ó¼ºµéÀÌ °³Ã¼¿¡ ¹Ý¿µµÈ´Ù.
function ¹®ÀåÀ» »ç¿ëÇϸé
<SCRIPT>
myCompany={name:'Trio',phone:'123456789',total:25};
function addMemb(obj,x){obj.total+=x} // function ¹®ÀåÀ» »ç¿ë
addMemb(myCompany,5);
document.write(myCompany.name+', ÃÑ¿ø='+myCompany.total+', ÀüȹøÈ£='+myCompany.phone);
</SCRIPT>
addMemb´Â º¯¼ö°¡ ¾Æ´Ï°í ¼ø¼ö ±â´ÉÇÔ¼öÀÌ´Ù.
±â´ÉÇÔ¼ö´Â ³×½ºÆà ¿¹Á¦
¿øÄ¢ÀûÀ¸·Î ±â´ÉÇÔ¼ö´Â ³×½ºÆà µÉ ¼ö ¾ø´Âµ¥ ´ÙÀ½°ú °°Àº Ư¼öÇÑ °æ¿ì¿¡´Â ³×½ºÆÃÀÌ °¡´ÉÇÏ´Ù.
¾ÕÀÇ µÎ ±Ý¾×Àº ¿ø, µÚÀÇ µÎ ±Ý¾×Àº ´Þ¶ó, ȯÀ² 1300
<SCRIPT>
function KrWon(won1,won2,dollar1,dollar2){
function exchange(amount){
return amount*1300;
}
return won1+won2+exchange(dollar1)+exchange(dollar2);
}
document.write('±Ý¾× ÇÕ°è='+KrWon(10000,20000,200,300));
</SCRIPT>
addMemb´Â ±â´ÉÇÔ¼ö¸¦ ÂüÁ¶ÇÏ´Â ÀÏÁ¾ÀÇ º¯¼öÀÌ´Ù.
³»ºÎ ±â´ÉÇÔ¼öÀÇ È£ÃâÀº ÇØ´ç ¿ÜºÎ ±â´ÉÇÔ¼ö ¼Ó¿¡¼¸¸ °¡´ÉÇÏ´Ù. ³»,¿Ü ±â´ÉÇÔ¼ö¿¡¼¸¸ ¼·Î º¯¼ö¿Í Àμöµé¸¸ »ç¿ëÇÒ ¼ö ÀÖ´Ù.
±â´ÉÇÔ¼öÀÌ À̺¥Æ®Ã³¸®ÀÚ ¿¹Á¦
Function °³Ã¼´Â À̺¥Æ®Ã³¸®ÀÚ¸¦ °¡Áú ¼ö ÀÖ´Ù.
<SCRIPT>
msg='¸¶¿ì½º¸¦ Ŭ¸¯Çß´Ù';
var clickTest=new Function("alert(msg)")
</SCRIPT>
<DIV onClick="clickTest()">¸¶¿ì½º¸¦ Ŭ¸¯ÇØ º¸¶ó</DIV>
À̺¥Æ®Ã³¸®ÀÚ´Â º¯¼ö¸¦ °¡Áú¼ö ¾øÀ¸³ª ±â´ÉÇÔ¼ö°¡ º¯¼ö¸¦ °¡Áö°í À̺¥Æ® ¹ß»ý½Ã ó¸®ÇØ ÁÙ ¼ö ÀÖ´Ù.
arguments(
Ä÷º¼Ç) ¼Ó¼º
IE3.02 NS3
¢¡
Function °³Ã¼¿¡ Àü´ÞµÈ Àμöµé(
arguments Ä÷º¼Ç)ÀÇ ¹è¿º¯¼ö¸¦ ¹ÝȯÇÑ´Ù.
¹®¹ý: [cVal=][functionObj.]arguments
ÄÚµå: myFunc=function(a,b,c){return arguments+' | '+arguments.length};document.write(myFunc(1,2,3))
°á°ú:
arguments.
callee ¼Ó¼º
IE NS4
¢¡
Function °³Ã¼ÀÇ º»¹®¿¡ ÇØ´çÇÏ¸ç ½ÇÇà ÁßÀÎ
Function °³Ã¼ ¼Ó¿¡¼
Function °³Ã¼ ÀÚü ¹®ÀÚ¿À» ¹ÝȯÇÑ´Ù.
¹®¹ý: [sVal=][functionObj.]arguments.callee
ÄÚµå:myFunc=function(a,b,c){return arguments.callee};document.write(myFunc(1,2,3))
°á°ú:
arguments.
caller ¼Ó¼º
IE3.02 NS3
¢¡ ÇöÀç ±â´ÉÇÔ¼ö¸¦ ºÒ·¯¿Â ±â´ÉÇÔ¼ö¿¡ ´ëÇÑ ÂüÁ¶¸¦ ¹ÝȯÇÑ´Ù.
¹®¹ý: [sVal=][functionObj.]arguments.caller
ÄÚµå:myFunc=function(a,b,c){return (myFunc.caller)};function showit(){document.write(myFunc(1,2,3))};showit()
°á°ú:
arguments.
length ¼Ó¼º
IE3.02 NS
¢¡ ÇöÀç ½ÇÇà ÁßÀÎ ±â´ÉÇÔ¼ö¿¡ Àü´ÞµÈ ÀμöÀÇ °¹¼ö¸¦ ¹ÝȯÇÑ´Ù.
¹®¹ý: [iVal=][functionObj.]arguments.length
ÄÚµå: myFunc=function(a,b,c){return arguments.length};document.write(myFunc(1,2,3))
°á°ú:
arity ¼Ó¼º
IE4 NS4
¢¡ ±â´ÉÇÔ¼ö¿¡ Àü´ÞÀ» ±â´ëÇÏ´Â ÀμöÀÇ °¹¼ö¸¦ ¹ÝȯÇÑ´Ù. ±×·¯³ª ¾Æ·¡
functionObj.
length¸¦ »ç¿ëÇ϶ó.
arguments.
length´Â ½ÇÁ¦ÀûÀ¸·Î Àü´ÞµÈ ÀμöÀÇ °¹¼öÀε¥ ¹ÝÇØ,
arity´Â ±â´ÉÇÔ¼ö ¼±¾ð¿¡ ¸í½ÃµÈ ÀμöÀÇ °¹¼öÀÌ´Ù.
¹®¹ý: [iVal=][functionObj.]arity
ÄÚµå: function arityTest(a,b,c){return(arity)};document.write(arityTest(1,2))
°á°ú:
constructor ¼Ó¼º
IE3.02 NS3
¢¡
Function °³Ã¼ÀÇ
prototypeÀ» ¸¸µå´Â ±â´ÉÇÔ¼ö¸¦ ÁöÁ¤ÇÑ´Ù.
ÀÌ´Â ±â´ÉÇÔ¼ö À̸§À» Æ÷ÇÔÇÏ´Â ¹®ÀÚ¿ÀÌ ¾Æ´Ï°í
Function °³Ã¼¿¡ ´ëÇÑ Á÷Á¢ÀûÀÎ ÂüÁ¶ÀÌ´Ù.
¹®¹ý: functionObj.constructor[=sVal]
ÄÚµå: document.write(clickTest.prototype.constructor)
°á°ú:
length ¼Ó¼º
IE3.02 NS3
¢¡ ±â´ÉÇÔ¼ö¿¡ Àü´ÞÀ» ±â´ëÇÏ´Â ÀμöÀÇ °¹¼ö¸¦ ¹ÝȯÇÑ´Ù.
arguments.
length´Â ½ÇÁ¦ÀûÀ¸·Î Àü´ÞµÈ ÀμöÀÇ °¹¼öÀε¥ ¹ÝÇØ,
functionObj.
length´Â ±â´ÉÇÔ¼ö ¼±¾ð¿¡ ¸í½ÃµÈ ÀμöÀÇ °¹¼öÀÌ´Ù.
¹®¹ý: [iVal=]functionObj.length
ÄÚµå: document.write(KrWon.length)
°á°ú:
prototype ¼Ó¼º
IE3.02 NS3
¢¡ ¼³Á¤ÀÚ°¡ ¸Þ¼µå¿Í ¼Ó¼ºµéÀ» Ãß°¡ÇÒ ¼ö ÀÖ´Â °³Ã¼ÀÇ Å¬·¡½ºÀÇ Ãʱ⠼³Á¤¿¡ ´ëÇÑ
prototypeÀ» ³ªÅ¸³½´Ù.
ÀÌ
prototype ¼Ó¼ºÀ¸·Î °³Ã¼¿¡ Ãß°¡ÀûÀÎ ¼Ó¼ºµé°ú ¸Þ¼µåµéÀ» Á¦°øÇÒ ¼ö ÀÖ´Ù.
Object.
prototype ¼Ó¼ºÀ» Âü°íÇ϶ó.
¹®¹ý: functionObj.prototype.name[=sVal]
ÄÚµå: document.write(KrWon.prototype.constructor)
°á°ú:
apply ¸Þ¼µå
IE5 NS4.06
¢¡ ´Ù¸¥
Function °³Ã¼ÀÇ ¸Þ¼µå¸¦ Àû¿ëÇÏ¿© ±× °³Ã¼¸¦ ÇöÀç °³Ã¼·Î ´ëüÇÑ´Ù.
¹®¹ý: functionObj.apply(thisObj[,arguArray})
¹®¹ý: Function apply([thisObj:Object[,arguArray:{Array|arguments}]])
call ¸Þ¼µå
IE5 NS4.06
¢¡ °³Ã¼ÀÇ ¸Þ¼µå¸¦ È£ÃâÇÏ¿© ´Ù¸¥ °³Ã¼·Î ºÎÅÍ ÇöÀç °³Ã¼·Î ´ëüÇÑ´Ù.
¹®¹ý: functionObj.call(thisObj[,argument1[,argument2[,,...argumentN]]])
¹®¹ý: [Function] call([thisObj[:Object[,argument1[,argument2[, ... [,argumentN]]]]])
toSource ¸Þ¼µå
IE4 NS4.06
¢¡ ÁöÁ¤µÈ
Function °³Ã¼¸¦ »ý¼ºÇϴµ¥ »ç¿ëµÈ ¼Ò½ºÄڵ带 ¹ÝȯÇÏ°í, ÀϹÝÀûÀ¸·Î JavaScript¿¡ ÀÇÇØ Á÷Á¢ È£ÃâµÈ´Ù.
ÀÌ´Â
Object.
toSource ¸Þ¼µå¸¦ µ¤¾î¾º¿î´Ù.
¹®¹ý: functionObj.toSource()
ÄÚµå: document.write(calcAverage.toSource())
toString ¸Þ¼µå
IE4 NS3
¢¡
Function °³Ã¼ÀÇ ¹®ÀÚ¿À» ¹ÝȯÇÏ°í,
Object.
toString ¸Þ¼µå¸¦ µ¤¾î ¾º¿î´Ù.
¹®¹ý: functionObj.toString()
ÄÚµå: document.write(clickTest.toString())
°á°ú:
ÄÚµå: document.write(calcAverage.toString())
°á°ú:
valueOf ¸Þ¼µå
IE4 NS3
¢¡
toString ¸Þ¼µå¿Í °°ÀÌ
Function °³Ã¼ÀÇ ¼Ò½ºÄڵ带 ´ëÇ¥ÇÏ´Â ±â´ÉÇÔ¼ö ±× ÀÚü¸¦ ¹®ÀÚ¿°ªÀ¸·Î ¹ÝȯÇÑ´Ù.
Object.
valueOf ¸Þ¼µå¸¦ µ¤¾î ¾º¿î´Ù.
¹®¹ý: functionObj.valueOf()
ÄÚµå: document.write(clickTest.valueOf())
°á°ú: