4-3 字典?件

@몺}COH`ǪƭȨӶiޡAOr媫]Dictionary^OHrӶiޡ]άOHrꬰȡ^AҥHڭ̥iHNr媫ݦOѦrrꪺYANO@r@ˡAڭ̥iH@Ӧr]εJ^AM۴Nr]εJ^C

nͤ@Ӧr媫Aϥ new ActiveXObject("Scripting.Dictionary") ͤ@ӪŪr媫AMAϥ Add() kӱNY@Ӥ@ӥ[ihAҦpG

Example]dict01.htm^G

dҭlXpUG

lɡ]dict01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>r媫󪺫غck</h2>
<hr>

<script>
capitalOf = new ActiveXObject("Scripting.Dictionary");	// غc@ӪŪr媫
capitalOf.Add("Taiwan", "Taipei");		// [J 1 ӹY
capitalOf.Add("USA", "Washington");		// [J 2 ӹY
capitalOf.Add("Korea", "Seoul");		// [J 3 ӹY
document.writeln("capitalOf(\"Taiwan\") = " + capitalOf("Taiwan") + "<br>");
document.writeln("capitalOf(\"USA\") = " + capitalOf("USA") + "<br>");
document.writeln("capitalOf(\"Korea\") = " + capitalOf("Korea") + "<br>");
</script>

<hr>
</body>
</html>

bWzdҤACӦr媫󪺯ެO@ӦrAҦp "Taiwan" N "Taipei"A] capitalOf("Taiwan") ȴNO "Taipei"CЪ`NO capitalOf("Taiwan") OΤpAAOΤA]AOΦb}C^C

Hint
r媫S٬p}C]Associative Arrays^ANӦrꤧpYAoM Perl Hash ƵcO@˪C

bHUdҤAڭ̷|Ъˬdr媫󪺦UӹYAҥHڭ̥wq@ӦLXr媫󪺨ơApUG

lɡ]listDict.js^G]ǦϰUYi^
// CLrܼ
function listDict(dict, dictName){
	allKeys = (new VBArray(dict.Keys())).toArray();   // X
	for (var i=0; i<dict.Count; i++)
		document.writeln(dictName+"(\"<font color=blue>"+allKeys[i]+"</font>\") = <font color=red>"+dict(allKeys[i])+"</font><br>");
}

䤤 dict.Count Nr媫󪺶حӼơA dict.Keys() N|bU廡CھڳoӨơAڭ̴NiHܤKaLX@Ӧr媫󪺹YC

Pr媫k٦ Remove()]RȡAPɤ]Rr^BKey()]ȡ^M Item()]ܹȡ^AШUCdҡG

Example]dict02.htm^G

dҭlXpUG

lɡ]dict02.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>r媫 Remove()BKey() M Item() k</h2>
<hr>

<script src=listDict.js></script>
<script>
countryOf = new ActiveXObject("Scripting.Dictionary");	// غc@ӪŪr媫
countryOf.Add("Taipei", "Taiwan");	// [J 1 ӹY
countryOf.Add("San Francisco", "USA");	// [J 2 ӹY
countryOf.Add("Tokyo", "Japan");	// [J 3 ӹY
countryOf.Add("Seoul", "Korea");	// [J 4 ӹY
countryOf.Remove("Tokyo");		//  Tokyo ȡ]ιr^
countryOf.Key("Taipei") = "Hsinchu";	// N Taipei ȧ令 Hsinchu
countryOf.Item("Tokyo") = "Nippon";	// N Tokyo ҹr令 Nippon
listDict(countryOf, "countryOf");
</script>

<hr>
</body>
</html>

ڭ̤]iHϥ Keys() M Items() ӨX@Ӧr媫ȩMȡAҦpG

Example]dict03.htm^G

dҭlXpUG

lɡ]dict03.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>ϥ Keys() M Items() Xr媫ȩM</h2>
<hr>

<script>
capitalOf = new ActiveXObject("Scripting.Dictionary");	// غc@ӪŪr媫
capitalOf.Add("Taiwan", "Taipei");		// [J 1 ӹY
capitalOf.Add("USA", "Washington");		// [J 2 ӹY
capitalOf.Add("Korea", "Seoul");		// [J 3 ӹY

document.writeln("<p>ϥ Keys() XȡG<br>");
allKeys = (new VBArray(capitalOf.Keys())).toArray();   // X
for (i=0; i<capitalOf.Count; i++)
	document.writeln(allKeys[i]+"<br>");
	
document.writeln("<p>ϥ Items() XȡG<br>");	
allItems = (new VBArray(capitalOf.Items())).toArray();   // X
for (i=0; i<capitalOf.Count; i++)
	document.writeln(allItems[i]+"<br>");
</script>

<hr>
</body>
</html>

bWzdҤA Keys() Items() ҶǦ^äઽϥΡAন VBArray AA toArray() ন JavaScript }CAoةs٨@kA̲qO]r媫󥻨ӴNO VBScript تAb JavaScript]YaAӬOLnJScript^ϦӬOsi~[JA]ӫܦh䴩Ţnb VBScript vUAҥH]unNN@UC

`Ψr媫󪺤k٦ Exists()]լYȬO_sb^M RemoveAll]RҦء^AШUCdҡG

Example]dict04.htm^G

dҭlXpUG

lɡ]dict04.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>ϥ Exists() M RemoveAll() r媫󪺤k</h2>
<hr>

<script src=listDict.js></script>
<script>
capitalOf = new ActiveXObject("Scripting.Dictionary");	// غc@ӪŪr媫
capitalOf.Add("Taiwan", "Taipei");		// [J 1 ӹY
capitalOf.Add("USA", "Washington");		// [J 2 ӹY
capitalOf.Add("Korea", "Seoul");		// [J 3 ӹY
listDict(capitalOf, "capitalOf");		// CLr媫

key="Taiwan";
if (capitalOf.Exists(key))	//  "Taiwan" O_Or媫 capitalOf 
	document.writeln("\"" + key + "\" is a key of \"capitalOf\".<br>");
else
	document.writeln("\"" + key + "\" is NOT a key of \"capitalOf\".<br>");
key="Japan";
if (capitalOf.Exists(key))	//  "Japan" O_Or媫 capitalOf 
	document.writeln("\"" + key + "\" is a key of \"capitalOf\".<br>");
else
	document.writeln("\"" + key + "\" is NOT a key of \"capitalOf\".<br>");
	
capitalOf.RemoveAll();			// RҦ
listDict(capitalOf, "capitalOf");	// CLr媫]ӦLXӡA]ҦسQRFI^
</script>

<hr>
</body>
</html>


JavaScript {]pPΡGΩΤ