4-2 �}�C���󪺤�k

JavaScript ¹ï°}¦Cª«¥ó´£¨Ñ¤F«Ü¦h¬ÛÃöªº¤èªk¡A¦Cªí¦p¤U¡G

¤èªk»¡©ú
concat()¶Ç¦^¤@­Ó¥Ñ¨â­Ó©Î¨â­Ó¥H¤W°}¦C¨Ã±Æ¦Ó¦¨ªº·s°}¦C
join()¶Ç¦^¤@­Ó¦r¦ê­È¡A¥¦¬O¥Ñ°}¦C¤¤ªº©Ò¦³¤¸¯À¦ê³s¦b¤@°_©Ò²Õ¦¨¡A¨Ã¥B¥Î¯S©wªº¤À¹j¦r¤¸¨Ó¤À¹j
pop()²¾°£°}¦Cªº³Ì«á¤@­Ó¤¸¯À¡A¨Ã±N¥¦¶Ç¦^
push()ªþ¥[·s¤¸¯À¨ì°}¦C§À³¡¡A¨Ã¶Ç¦^°}¦Cªº·sªø«×
reverse()¶Ç¦^¤@­Ó¤¸¯À¦ì¸m¤ÏÂ઺°}¦C
shift()²¾°£°}¦Cªº²Ä¤@­Ó¤¸¯À¡A¨Ã±N¥¦¶Ç¦^
slice()¶Ç¦^°}¦Cªº¤@­Ó°Ï¬q
splice()²¾°£°}¦C¤¤ªº¤¸¯À¡A¨Ã¨Ì»Ý­n¦b­ì¦ì´¡¤J·s¤¸¯À¡AµM«á¶Ç¦^³Q§R°£ªº¤¸¯À
sort()¶Ç¦^¤@­Ó¤¸¯À¤w±Æ§Ç¹Lªº°}¦C
toString()¶Ç¦^¤@­Óª«¥ó¡]©Î°}¦C¡^ªº¦r¦êªí¥Üªk
unshift()¦b°}¦C¶}©l³B´¡¤J«ü©wªº¤¸¯À¡A¨Ã¶Ç¦^¦¹°}¦C

Á|¨Ò¨Ó»¡¡A¹ï©ó¤@­Ó°}¦C¡A§Ú­Ì¥i¥Î sort() ¨Ó¶i¦æ±Æ§Ç¡A©Î¥Î reverse() ¨Ó¶i¦æ¤Ï±Æ¡A½d¨Ò¦p¤U¡G

Example¡]arraySort01.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

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

<body>
<h2 align=center>°}¦Cªº¤èªk¡Gsort() ©M reverse()</h2>
<hr>

<script src=listArray.js></script>
<script>
myArray = ["First", "3.1415926", "Third", 4];
document.writeln("<p>­ì¨Óªº°}¦C¡G<br>");
listArray(myArray, "myArray");
document.writeln("<p>±Æ§Ç«áªº°}¦C¡G<br>");
listArray(myArray.sort(), "myArray");
document.writeln("<p>¤Ï±Æ«áªº°}¦C¡G<br>");
listArray(myArray.reverse(), "myArray");
</script>
<hr>
</body>
</html>

±q¤W­z½d¨Ò¥i¥H¬Ý¥X¡Asort() ·|¥ý±N¼Æ­ÈÂন¦r¦ê¡A¦A¶i¦æ¦r¦êªº±Æ§Ç¡C­Y­n¶i¦æ¼Æ­Èªº±Æ§Ç¡A´N¥²¶·©w¸q¤@­Ó¤ñ¸û¨ç¼Æ¡A¨Ã±N¦¹¤ñ¸û¨ç¼Æ°e¶i sort()¡A½Ð¨£¤U¦C½d¨Ò

Example¡]arraySort02.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

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

<body>
<h2 align=center>°}¦Cªº¤èªk¡G¦p¦ó©w¸q sort() ªº¤ñ¸û¨ç¼Æ</h2>
<hr>

<script>
// ©w¸q¤@­Ó¤ñ¸û¨ç¼Æ
function comparisonFunction(a, b){   
	return(a-b);
}
myArray = ["80", "9", "700", 40, 1, 5, 200];
document.write("­ì©l°}¦C¡G" + myArray +"<br>")
document.write("¨Ì¦r¦ê±Æ§Çªºµ²ªG¡G" + myArray.sort() +"<br>")
document.write("¨Ì¼Æ­È±Æ§Çªºµ²ªG¡G" + myArray.sort(comparisonFunction) +"<br>")
</script>

<hr>
</body>
</html>

¥Ñ¦¹½d¨Ò¥iª¾¡A¥u­n§Ú­Ì©w¸q¤F¤@­Ó©ú½Tªº¤ñ¸û¨ç¼Æ¡Asort() ´N·|®Ú¾Ú¦¹¨ç¼Æ¨Ó¶i¦æ¤ñ¸û¤Î±Æ§Ç¡C¡]¥t¥~¡A¦b¤W¨Ò¤¤¡A·í§Ú­Ì¨Ï¥Î¥[¸¹±N°}¦C©M¦r¦ê¨Ã±Æ®É¡AJavaScript ·|±N°}¦CÂà´«¦¨¥Ñ³r¸¹¹j¶}ªº¦r¦ê¡A«K©ó¨Ã±Æ¡C¡^

Hint
¦pªG¤@­Ó°}¦Cªº¨C¤@­Ó¤¸¯À³£¬O¼Æ­È¡AJavaScript ¦b°õ¦æ sort() ®É¡AÁÙ¬O·|¨Ì·Ó¦r¦ê¨Ó±Æ§Ç¡A³o¬O¯S§O­nª`·Nªº¦a¤è¡C

¦³¤@¨Ç¤èªk¥i¥Î¨Ó¼W¤j©ÎÁY¤p ¤@­Ó°}¦C¡A¨Ò¦p pop()¡Bpush()¡Bshift() ©M unshift()¡A½Ð¨£¤U¨Ò¡G

Example¡]arrayPop01.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

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

<body>
<h2 align=center>°}¦Cªº¤èªk¡Gpop(), push(), shift(), unshift()</h2>
<hr>

<script>
myPet = ["¹«", "¤û", "ªê", "¨ß"];
document.writeln("myPet = " + myPet + "<br>");
//´ú¸Õ pop()
cmd = "popped = myPet.pop()";
eval(cmd);
document.writeln("<p>After ¡u" + cmd + "¡v¡G<br>");
document.writeln("myPet = " + myPet + "<br>");
document.writeln("poped = " + popped + "<br>");
//´ú¸Õ push()
cmd = 'elementCount = myPet.push("Às", "³D")';
eval(cmd);
document.writeln("<p>After ¡u" + cmd + "¡v¡G<br>");
document.writeln("myPet = " + myPet + "<br>");
document.writeln("elementCount = " + elementCount + "<br>");
//´ú¸Õ shift()
cmd = "shifted = myPet.shift()";
eval(cmd);
document.writeln("<p>After ¡u" + cmd + "¡v¡G<br>");
document.writeln("myPet = " + myPet + "<br>");
document.writeln("shifted = " + shifted + "<br>");
//´ú¸Õ unshift()
cmd = 'myPet.unshift("ª¯", "½Þ")';
eval(cmd);
document.writeln("<p>After ¡u" + cmd + "¡v¡G<br>");
document.writeln("myPet = " + myPet + "<br>");

</script>

<hr>
</body>
</html>

§Ú­Ì¤]¥i¥H¨Ï¥Î toString()¡]±N¯x°}Âন¥Ñ³rÂI¬Û³sªº¦r¦ê¡^©Î join()¡]±N¯x°}Âন¥Ñ¯S©w²Å¸¹¬Û³sªº¦r¦ê¡^¨Ó±N°}¦CÂন³æ¤@¦r¦ê¡A½d¨Ò¦p¤U¡G

Example¡]arrayJoin01.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

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

<body>
<h2 align=center>°}¦Cªº¤èªk¡GtoString() ©M join()</h2>
<hr>

<script>
myPet = ["¹«", "¤û", "ªê", "¨ß"];
document.writeln("myPet = " + myPet + "<br>");
document.writeln("myPet.toString() = " + myPet.toString() + "<br>");
document.writeln("myPet.join() = " + myPet.join() + "<br>");
document.writeln("myPet.join(', ') = " + myPet.join(', ') + "<br>");
document.writeln("myPet.join(' + ') = " + myPet.join(' + ') + "<br>");
</script>

<hr>
</body>
</html>

¦³¤W­z½d¨Ò¥i¥H¬Ý¥X¡AmyPet.toString() ©M myPet.join() ±o¨ìªºµ²ªG¬O¤@¼Ëªº¡C

¥t¤@­Ó©M join() ¥\¯à¬Û¤Ïªº¨ç¼Æ¬O split()¡A¥i¥H±N¦r¦ê¤Á¶}¨Ó¡AÂন°}¦C¡A¦¹¥~¡A§Ú­Ì¤]¥i¥H¨Ï¥Î concat() «ü¥O±N¨â­Ó°}¦C³s±µ°_¨Ó¡A§Î¦¨¤@­Ó§ó¤jªº°}¦C¡A½d¨Ò¦p¤U¡G

Example¡]arrayConcat01.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

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

<body>
<h2 align=center>°}¦Cªº¤èªk¡Gsplit() ©M concat()</h2>
<hr>

<script src="listArray.js"></script>
<script>
str1="Áé­Û¡B³¯´¸";
str2="³¯¦¿§ø¡BªL¬F·½¡B±i¥Ã´Ë";
array1=str1.split('¡B');	// ±N¦r¦ê©î¦¨°}¦C
array2=str2.split('¡B');	// ±N¦r¦ê©î¦¨°}¦C
document.writeln("Array 1:<br>");
listArray(array1, 'array1');
document.writeln("Array 2:<br>");
listArray(array2, 'array2');
document.writeln("<p>±N¤W­z¨â­Ó°}¦C¨Ã±Æªºµ²ªG¡G<br>");
document.writeln("Array 3:<br>");
array3=array1.concat(array2);
listArray(array3, 'array3');
</script>

<hr>
</body>
</html>

¹ï©ó¤º«Øªºª«¥ó¡A§Ú­Ì¥i¥H¨Ï¥Î prototype Äݩʨөw¸q·sªº¤èªk¡C¨Ò¦p¡A¹ï©ó¤@­Ó°}¦Cª«¥ó¡A¦pªG§Ú­Ì­n©w¸q¤@­Ó·s¤èªk max() ¨Ó¶Ç¦^°}¦C¤¤³Ì¤jªº­È¡A¥i¨£¤U¦C½d¨Ò¡G

Example¡]prototype01.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

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

<body>
<h2 align=center>©w¸q°}¦Cªº¤èªk</h2>
<hr>

<script src=listArray.js></script>
<script>
function arrayMax( ){
	var i, max = this[0];
	for (i=1; i<this.length; i++)
		if (max<this[i])
			max=this[i];
	return(max);
}
Array.prototype.max = arrayMax;		// ©w¸q arrayMax ¬°°}¦C¤èªk max ©Ò©I¥sªº¨ç¼Æ
myArray = new Array(4, 9, 2, 1);
document.write("myArray ªº¤¸¯À¡G<br>");
listArray(myArray, "myArray");		// ¦C¦L°}¦C¦U­Ó¤¸¯À
document.write("myArray ªº¤¸¯À³Ì¤j­È¡G" + myArray.max());	// ¦C¦L°}¦C³Ì¤j¤¸¯À
</script>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A½Ð¯S§Oª`·N¨âÂI¡G

¤U­±³o­Ó½d¨Ò¡A¦C¥X¤@­Ó°}¦Cªº©Ò¦³©Ê½è¡G

Example¡]arrayProp01.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

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

<body>
<h2 align=center>°}¦Cªº©Ê½è</h2>
<hr>

<script>
myArray = ["¸ê°T¨t", "¹q¾÷¨t", "§÷®Æ¨t"];
// ¦CÁ|°}¦C¤¤ªº©Ò¦³ÄÝ©Ê
for (prop in myArray)
	document.write("<br>myArray." + prop + " = " + myArray[prop]);
</script>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A¥i¥Hµoı JavaScript ¦ü¥G§â 0, 1, 2 ·í¦¨¬O°}¦Cª«¥óªº©Ê½è¡A¦ý¨Æ¹ê¤W¡A§Ú­Ì¦b¨ú¥Î°}¦Cªº¤¸¯À®É¡AÁÙ¬O¥²¶·¨Ï¥Î myArray[2] ©Î¬O myArray["2"] µ¥¡A¦Ó¤£¯à¨Ï¥Î myArray.2¡A¨Ì¦¹Ãþ±À¡C


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