5-1 �ܼƦ��Ľd��

¡u¨ç¼Æ¡v¡]Functions¡^¬O©Ò¦³ªºµ{¦¡»y¨¥¥²³Æªº¥\¯à¡CJavaScript ªº¨ç¼Æ¥i¥H¤À¦¨¤U¦C´XÃþ¡G ¥»¤p¸`±N°w¹ï¨Ï¥ÎªÌ©w¸qªº¨ç¼Æ¨Ó¶i¦æ»¡©ú¡C

Hint
¤@¯ë¦Ó¨¥¡A§Ú­Ì¨Ï¥Î¡u¨ç¼Æ¡v¨Ó¥Nªí¼Æ¾Ç¨ç¼Æ¡A¨Ò¦p¤T¨¤¨ç¼Æµ¥¡A¦Ó¥Î¡u¨ç¦¡¡v¨Ó¥Nªíµ{¦¡½X¤¤ªº¨ç¼Æ¡C¦ý¦b¥»®Ñ¤¤¡A¨Ã¨S¦³¦¹ÄYÂÔªº°Ï¤À¡A¦]¦¹¡u¨ç¦¡¡v©M¡u¨ç¼Æ¡v¬O¥i¥H¥æ´«¨Ï¥Î¡A³q±`³£¬O«üµ{¦¡½X©Ò©w¸qªº¨ç¼Æ¡C

¨ç¼Æªº©w¸q¥]§t¨ç¼Æ¦WºÙ (Function name) ¤Î¿é¤J¤Þ¼Æ (Input arguments)¡A¨ä°ò¥»®æ¦¡¦p¤U¡G

function functionName(InputArguments) { JavaScript statements... ... return(output) // ¥i¬Ù²¤ }
­«ÂI»¡©ú¦p¤U¡G ¦b¥H¤U½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î¨â­Ó¨ç¼Æ¨ÓÅã¥Ü²{¦b®É¶¡»P¬P´Á´X¡G

Example¡]timeDisplay01.htm¡^¡G

¦¹½d¨Ò­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]timeDisplay01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>¨Ï¥Î¨ç¼Æ¨ÓÅã¥Ü²{¦b®É¶¡»P¬P´Á´X</h2>
<hr>

<script>
function currentTime(){		// ¦^¶Ç²{¦bªº®É¶¡
	var today = new Date();
	var hour = today.getHours();
	var minute = today.getMinutes();
	var second = today.getSeconds();
	var prepand = (hour>12)? "¤U¤È":"¤W¤È";
	hour = (hour>12)? hour-12:hour;
	return(prepand + hour + " ÂI " + minute + " ¤À " + second + " ¬í");
}

function currentDay(){		// ¦^¶Ç¤µ¤Ñ¬P´Á´X
	var today = new Date();
	var day = today.getDay();	// ¨ú±o¤µ¤Ñ¬O¬P´Á´X
	var conversion=["¤Ñ", "¤@", "¤G", "¤T", "¥|", "¤­", "¤»"];
	return("¬P´Á"+conversion[day]);
}
</script>

<script>
document.write('¤µ¤Ñ¬O' + currentDay() + '¡A¥Ø«e®É¶¡¬O' + currentTime()+ '¡I');
</script>

<hr>
</body>
</html>

¦b¨Ï¥Î¨ç¼Æ®É¡A§Ú­Ì¥²¶·¦³¡u§½³¡Åܼơv©M¡u¥þ°ìÅܼơvªº·§©À¡A¥i²³æ©w¸q¦p¤U¡G

¤@¯ë¦Ó¨¥¡A¦pªG§Ú­Ì¥ÎªºÅܼƥu¦b¤@­Ó¨ç¼Æªº¦³®Ä½d³ò¡]Scope¡^¤º¹B§@¡A«h¦¹ÅܼƳq±`´NÀ³¸Ó³Q³]©w¬°§½³¡ÅܼơA¥H§K¦b¨ç¼Æ°õ¦æ«á²£¥Í°Æ§@¥Î¡A¼vÅT¨ì¨ä«áµ{¦¡½Xªº°õ¦æ¡C¨Ò¦p¡G

Example¡]scopeOfVariable01.htm¡^¡G

¦¹½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]scopeOfVariable01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>Åܼƪº¦³®Ä½d³ò</h2>
<hr>

<script>
function testScope() {
	var x=5;	// §½³¡ÅܼÆ
	y=5;	// ¥þ°ìÅܼÆ
	document.write("Inside function:<br>");
	document.write("x = "+x+"<br>");
	document.write("y = "+y+"<br>");
}
</script>

<script>
x = 10;	// ¥þ°ìÅܼÆ
y = 10;	// ¥þ°ìÅܼÆ
document.write("Before invoking function:<br>");
document.write("x = "+x+"<br>");
document.write("y = "+y+"<br>");
testScope();
document.write("After invoking function:<br>");
document.write("x = "+x+"<br>");
document.write("y = "+y+"<br>");
</script>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡Ax ©M y ³£¬O©w¸q¬°¥þ°ìÅܼơA¦ý¬O¦b¨ç¼Æ testScope() ¤º¡A¥t¤@­Ó¦P¦Wªº°Ï°ìÅÜ¼Æ x ¾B½ª¤F­ì¥ýªº¥þ°ìÅÜ¼Æ x¡A¦]¦¹¦b¨ç¼Æ¤º¦L¥X¨Óªº x ­È¬O 5¡C¦ý¬OÂ÷¶}¨ç¼Æ¤§«á¡Ax ÁÙ¬O¤@­Ó¥þ°ìÅܼơA¨ä­ÈÁÙ¬O 10¡CÅÜ¼Æ y «h¬O¥þ°ìÅܼơA¦]¦¹­Y¦b¨ç¼Æ¤¤§ïÅܨä­È¡A¤]·|¤Ï¬M¨ì¨ç¼Æ¥~ªº y¡C

¤@­Ó±`µo¥Íªº¿ù»~¡A´N¬O¨S¦³§â¨ç¼Æ¤º³¡ªºÅܼƳ]©w¦¨§½³¡ÅܼơA¾É­P¦¹ÅܼƷ|Ä~©Ó©Î¼vÅT¥~³¡Åܼƪº­È¡C¦]¦¹¡A¬°¤F´î¤Ö°£¿ùªº®É¶¡¡A©Ò¦³¨ç¼Æªº¤º³¡ÅܼơA¦b²Ä¤@¦¸¨Ï¥Î®É³Ì¦n¥[¤W var¡A¤w½T»{¨ä¦³®Ä½d³ò¥u¦b¦¹¨ç¼Æ¤º¡C

Hint
¤@­Ó¼gµ{¦¡ªº¦n²ßºD¡A·|¬Ù¥h«áÄòµL¼Æªº°£¿ù®É¶¡¡A©Ò¥Hµ§ªÌ¦b¦¹¤£¹½¨ä·Ð¡B¦A´£¿ô¤@¦¸¡G¨ç¼Æ¤ºªºÅܼơA­nºÉ¶q¥[¤W var¡A¥H½T«O¤£·|©M¨ç¼Æ¥~ªºÅܼƬ۽ġC

¦b¨ç¼Æ¥~³¡©w¸qªºÅܼơAµL½×¬O§_¦³¥[ var¡A³£·|³Qµø¬°¬O¥þ°ìÅܼơA¦]¦¹¥i¥H¤£¥²¨è·N¦A¥h¥[ var¡C


JavaScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©óºô­¶¥Î¤áºÝ