5-1 璁婃暩?夋?绡勫?

ㄧ计Functions琌┮Τ祘Α粂ēゲ称JavaScript ㄧ计だΘ碭摸 セ竊盢皐癸ㄏノ﹚竡ㄧ计ㄓ秈︽弧

Hint
τēиㄏノㄧ计ㄓ计厩ㄧ计ㄒàㄧ计单τノㄧΑㄓ祘Α絏いㄧ计セい⊿Τ腨略跋だㄧΑ㎝ㄧ计琌ユ传ㄏノ硄盽常琌祘Α絏┮﹚竡ㄧ计

ㄧ计﹚竡ㄧ计嘿 (Function name) の块ま计 (Input arguments)ㄤ膀セΑ

function functionName(InputArguments) { JavaScript statements... ... return(output) // 菠 }
翴弧 絛ㄒいиㄏノㄢㄧ计ㄓ陪ボ瞷丁籔琍戳碭

ExampletimeDisplay01.htm

絛ㄒ﹍絏

﹍郎timeDisplay01.htmη︹跋办ㄢī
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>ㄏノㄧ计ㄓ陪ボ瞷丁籔琍戳碭</h2>
<hr>

<script>
function currentTime(){		// 肚瞷丁
	var today = new Date();
	var hour = today.getHours();
	var minute = today.getMinutes();
	var second = today.getSeconds();
	var prepand = (hour>12)? "と":"と";
	hour = (hour>12)? hour-12:hour;
	return(prepand + hour + " 翴 " + minute + " だ " + second + " ");
}

function currentDay(){		// 肚さぱ琍戳碭
	var today = new Date();
	var day = today.getDay();	// 眔さぱ琌琍戳碭
	var conversion=["ぱ", "", "", "", "", "き", "せ"];
	return("琍戳"+conversion[day]);
}
</script>

<script>
document.write('さぱ琌' + currentDay() + 'ヘ玡丁琌' + currentTime()+ '');
</script>

<hr>
</body>
</html>

ㄏノㄧ计иゲ斗ΤЫ场跑计㎝办跑计阀├虏虫﹚竡

τē狦иノ跑计ㄧ计Τ絛瞅Scopeず笲玥跑计硄盽碞莱赣砆砞﹚Ы场跑计ㄧ计磅︽玻ネ捌ノ紇臫ㄤ祘Α絏磅︽ㄒ

ExamplescopeOfVariable01.htm

絛ㄒЧ俱﹍郎

﹍郎scopeOfVariable01.htmη︹跋办ㄢī
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>跑计Τ絛瞅</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>

瓃絛ㄒいx ㎝ y 常琌﹚竡办跑计琌ㄧ计 testScope() ず跋办跑计 x 綛姜办跑计 xㄧ计ずㄓ x 琌 5琌瞒秨ㄧ计ぇx 临琌办跑计ㄤ临琌 10跑计 y 玥琌办跑计璝ㄧ计いэ跑ㄤ穦は琈ㄧ计 y

盽祇ネ岿粇碞琌⊿Τрㄧ计ず场跑计砞﹚ΘЫ场跑计旧璓跑计穦膥┯┪紇臫场跑计搭ぶ埃岿丁┮Τㄧ计ず场跑计材Ωㄏノ程 var絋粄ㄤΤ絛瞅ㄧ计ず

Hint
糶祘Α策篋穦尿礚计埃岿丁┮掸ぃ菇ㄤ沸矗眶Ωㄧ计ず跑计璶荷秖 var絋玂ぃ穦㎝ㄧ计跑计侥

ㄧ计场﹚竡跑计礚阶琌Τ var常穦砆跌琌办跑计ぃゲㄨ種 var


JavaScript 祘Α砞璸籔莱ノノ呼ノめ狠