4-2 ????件?方?

JavaScript }C󴣨ѤFܦhkACpUG

k
concat()Ǧ^@ӥѨөΨӥHW}CñƦӦs}C
join()Ǧ^@ӦrȡAOѰ}CҦsb@_ҲզAåBίSwjrӤj
pop()}C̫@ӤAñNǦ^
push()[s}CAöǦ^}Cs
reverse()Ǧ^@Ӥm઺}C
shift()}CĤ@ӤAñNǦ^
slice()Ǧ^}C@ӰϬq
splice()}CAè̻ݭnb촡JsAMǦ^QR
sort()Ǧ^@ӤwƧǹL}C
toString()Ǧ^@Ӫ]ΰ}C^rܪk
unshift()b}C}lBJwAöǦ^}C

|ҨӻA@Ӱ}CAڭ̥i sort() ӶiƧǡAΥ reverse() ӶiϱơAdҦpUG

Example]arraySort01.htm^G

WzdҪlɦpUG

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

<body>
<h2 align=center>}CkGsort() M reverse()</h2>
<hr>

<script src=listArray.js></script>
<script>
myArray = ["First", "3.1415926", "Third", 4];
document.writeln("<p>Ӫ}CG<br>");
listArray(myArray, "myArray");
document.writeln("<p>Ƨǫ᪺}CG<br>");
listArray(myArray.sort(), "myArray");
document.writeln("<p>ϱƫ᪺}CG<br>");
listArray(myArray.reverse(), "myArray");
</script>
<hr>
</body>
</html>

qWzdҥiHݥXAsort() |NƭনrAAirꪺƧǡCYniƭȪƧǡANwq@ӤơAñNưei sort()AШUCd

Example]arraySort02.htm^G

WzdҪlɦpUG

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

<body>
<h2 align=center>}CkGpwq sort() </h2>
<hr>

<script>
// wq@Ӥ
function comparisonFunction(a, b){   
	return(a-b);
}
myArray = ["80", "9", "700", 40, 1, 5, 200];
document.write("l}CG" + myArray +"<br>")
document.write("̦rƧǪGG" + myArray.sort() +"<br>")
document.write("̼ƭȱƧǪGG" + myArray.sort(comparisonFunction) +"<br>")
</script>

<hr>
</body>
</html>

ѦdҥiAunڭ̩wqF@өTơAsort() N|ھڦƨӶiαƧǡC]t~AbWҤAڭ̨ϥΥ[N}CMrñƮɡAJavaScript |N}Cഫѳrj}rAKñơC^

Hint
pG@Ӱ}CC@ӤOƭȡAJavaScript b sort() ɡA٬O|̷ӦrӱƧǡAoOSOn`NaC

@ǤkiΨӼWjYp @Ӱ}CAҦp pop()Bpush()Bshift() M unshift()AШUҡG

Example]arrayPop01.htm^G

WzdҪlɦpUG

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

<body>
<h2 align=center>}CkGpop(), 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 + "vG<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 + "vG<br>");
document.writeln("myPet = " + myPet + "<br>");
document.writeln("elementCount = " + elementCount + "<br>");
// shift()
cmd = "shifted = myPet.shift()";
eval(cmd);
document.writeln("<p>After u" + cmd + "vG<br>");
document.writeln("myPet = " + myPet + "<br>");
document.writeln("shifted = " + shifted + "<br>");
// unshift()
cmd = 'myPet.unshift("", "")';
eval(cmd);
document.writeln("<p>After u" + cmd + "vG<br>");
document.writeln("myPet = " + myPet + "<br>");

</script>

<hr>
</body>
</html>

ڭ̤]iHϥ toString()]Nx}নѳrI۳sr^ join()]Nx}নѯSwŸ۳sr^ӱN}Cন@rAdҦpUG

Example]arrayJoin01.htm^G

WzdҪlɦpUG

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

<body>
<h2 align=center>}CkGtoString() 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>

WzdҥiHݥXAmyPet.toString() M myPet.join() o쪺GO@˪C

t@өM join() \ۤϪƬO split()AiHNr}ӡAন}CA~Aڭ̤]iHϥ concat() ONӰ}Cs_ӡAΦ@ӧj}CAdҦpUG

Example]arrayConcat01.htm^G

WzdҪlɦpUG

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

<body>
<h2 align=center>}CkGsplit() M concat()</h2>
<hr>

<script src="listArray.js"></script>
<script>
str1="ۡB";
str2="BLFBiô";
array1=str1.split('B');	// Nr}C
array2=str2.split('B');	// Nr}C
document.writeln("Array 1:<br>");
listArray(array1, 'array1');
document.writeln("Array 2:<br>");
listArray(array2, 'array2');
document.writeln("<p>NWzӰ}CñƪGG<br>");
document.writeln("Array 3:<br>");
array3=array1.concat(array2);
listArray(array3, 'array3');
</script>

<hr>
</body>
</html>

󤺫تAڭ̥iHϥ prototype ݩʨөwqskCҦpA@Ӱ}CApGڭ̭nwq@ӷsk max() ӶǦ^}C̤jȡAiUCdҡG

Example]prototype01.htm^G

WzdҪlɦpUG

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

<body>
<h2 align=center>wq}Ck</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;		// wq arrayMax }Ck max ҩIs
myArray = new Array(4, 9, 2, 1);
document.write("myArray G<br>");
listArray(myArray, "myArray");		// CL}CUӤ
document.write("myArray ̤jȡG" + myArray.max());	// CL}C̤j
</script>

<hr>
</body>
</html>

bWzdҤAЯSO`NIG

UoӽdҡACX@Ӱ}CҦʽG

Example]arrayProp01.htm^G

WzdҪlɦpUG

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

<body>
<h2 align=center>}Cʽ</h2>
<hr>

<script>
myArray = ["Tt", "qt", "ƨt"];
// C|}CҦݩ
for (prop in myArray)
	document.write("<br>myArray." + prop + " = " + myArray[prop]);
</script>

<hr>
</body>
</html>

bWzdҤAiHoı JavaScript G 0, 1, 2 O}C󪺩ʽAƹWAڭ̦bΰ}CɡA٬Oϥ myArray[2] άO myArray["2"] AӤϥ myArray.2A̦C


JavaScript {]pPΡGΩΤ