ºô­¶¼ÐÃD¡G ÅÜ¼Æ (Variable)
¡@



JavaScript¡@ªì¶¥±Ð¾Ç

http://taiwantc.com/js/js_tut_a3.htm

ÅÜ¼Æ (Variable)

ÅܼÆ
¸ê®Æ«¬ºA
°}¦C

ÅܼÆ
ÅܼƬO¥Î¨ÓÀx¦s©ÎŪ¨ú¤@¨Ç¸ê®Æ¡A ¥H¤è«K¶i¦æ¹Bºâ¡C

¼gªk¡G©w¸q¤@­ÓÅܼÆ

¥u©w¸q¤@­ÓÅܼƪº¦WºÙ¡A ¦ý¤£§Y®É«ü©w¥¦ªº¸ê®Æ¡G
var variable_name ;
©w¸q¤@­ÓÅܼƦWºÙ¡A ¦P®É«ü©w¥¦ªº¸ê®Æ¡G
var variable_name = data ;

var ¬O¥Î¨Ó©w¸qÅܼƪº¡A variable_name ¬OÅܼƪº¦WºÙ¡A data ¬O¥ô¦ó«¬ºAªº¸ê®Æ¡C ¨Ò¤l¡G©w¸q¤@­Ó¦r¦ê«¬ºAªºÅܼÆ

var myname = "Tom" ;

¨Ò¤l»¡©ú

¨Ò¤l¤¤©w¸q¤F¤@­ÓÅܼơA ÅܼƦWºÙ¬O myname ¡C ¦]¬°¸Ó¸ê®Æ¬O¥Î¤Þ¸¹¬AµÛ¡A ©Ò¥H¸ê®Æ«¬ºA¬O¦r¦ê (String)¡A ¸ê®Æ´N¬O¤@¥y Tom ¦r¦ê¡A ¦Ó myname ´N¬O¤@­Ó¦r¦ê«¬ºAªºÅܼƤF¡C

¨Ò¤l¡G©w¸q¤@­Ó¾ã¼Æ«¬ºAªºÅܼÆ

var myage = 16 ;

¨Ò¤l»¡©ú

¨Ò¤l¤¤©w¸q¤F¤@­ÓÅܼơA ÅܼƦWºÙ¬O myage ¡C §Ú¥Î myage ¨ÓÀx¦s¾ã¼Æ«¬ºA¡A ¦Ó¤£¬O¦r¦ê«¬ºAªº¸ê®Æ¡A ¦]¦¹¤£¥Î¤Þ¸¹¬AµÛ¡C ¸Ó¸ê®Æ«¬ºA¬O¾ã¼Æ (Integer)¡A ¸ê®Æ´N¬O¤@­Ó 16 ¾ã¼Æ¡A ¦Ó myage ´N¬O¤@­Ó¾ã¼Æ«¬ºAªºÅܼƤF¡C

ÅܼƦWºÙ¡]©Î¨ç¼Æ¦WºÙ¡^ªº©R¦W³W«h¡G

ÅܼƦWºÙ¥u¥i¥H¥Ñ­^¤å¦r¡B¼Æ¥Ø¦r¤Î©³½u (_)²Õ¦¨
ÅܼƦWºÙªº²Ä¤@­Ó¦r¤¸¤£¥i¥H¬O¼Æ¥Ø¦r¡C
¤£¥i¥H¬OÃöÁä¦r¡C ¨Ò¦p§A¤£¥i¥H¥Î function §@¬°¬Y­ÓÅܼƪº¦WºÙ¡A ¦]¬°¥¦¬O¤@­ÓÃöÁä¦r¡C

¨Ò¤l¡G

¦Xªkªº¦WºÙ¡G my_name , myName , g2 , _2
¤£¦Xªkªº¦WºÙ¦³¡G 01 , 1choice , var , function , Array

¨Ò¤l¡G©w¸qÅܼÆ




[/code]

°õ¦æ (1)

¸ê®Æªº«¬ºA (Data Type)
±`¨£ªº¸ê®Æ«¬ºA¦³¥H¤U´XºØ¡G

¸ê®Æ«¬ºA ¨Ò¤l »¡©ú
¦r¦ê (String)
var name = "Tom"; var model = "1001";
¦r¦ê³q±`¬O¥ÎÂù¤Þ¸¹ (") ©Î³æ¤Þ¸¹(')¬AµÛ¡A ¨Ò¤l¤¤ªí¥Ü¤F¤@­Ó Tom ¦r¦ê©M¤@­Ó 1001 ¦r¦ê¡C

¼Æ¥Ø (Number)
var age = 16; var pi = 3.141592653589793;
var expo = 54e7; var octal_num = 010;
var hex_num = 0x10;
¼Æ¥Ø¥i¥H¬O¹ê¼Æ©M¾ã¼Æ¡A ¾ã¼Æ¥]¬A10 ¶i¨î¡B8 ¶i¨î©M 16 ¶i¨î¡C ¼Æ¥Ø«e­±¬O 0 ¥Nªí 8 ¶i¨î¡A ¼Æ¥Ø«e­±¬O 0x ¥Nªí 16 ¶i¨î

¥¬ªL (Boolean)
var is_enabled = true; var ie3 = false;
¥¬ªL¸ê®Æ¥u¦³¨â­Ó­È¡A ´N¬O true ©ÎªÌ false ¥ô¦ó¤@­Ó¡A ¾A¦X¥Î§@¤@¨Ç¥u¦³¨â­Ó¥i¯àªº¹Bºâ¡C

ªÅ¥Õ (Null)
var no_content;
¸ê®Æ¨S¦³¤º®e¡A ¨Ò¦p§A¥u©w¸q¥¦Åܼƪº¦WºÙ¡A ¦ý¨S¦³«ü©w¤º®e¡A ¨º»ò¥¦´Nµ¥©ó null ¤F¡C

ª«¥ó (Object)
var dw = document.write; var myname = document.myform.myname;
³q±`¬O¬°¤F¤è«K¦Ó©w¸qª«¥óÅܼơA ¨Ò¦p¥i¥H¥Î dw ¥N´À documen.write¡A ¨º´N¤£¥Î¥´«Ü¦h¦r¤F

°}¦C (Array)
var even_num = new Array(2,4,6,8,10);
var fruit = new Array("apple","orange");
°}¦C¬O¥Î¨ÓÀx¦s¤@²Õ¬Û¦P«¬ºAªº¸ê®Æ¡A ¦b¥»½gµy«á¸Ô­z¡C

§A¥i¥HÀH®É«ü©wÅܼƪº¤º®e¡A ¥ç§Y¬O«ü©w¥¦ªº¸ê®Æ¡A ¦ýÀ³¯d·N¸Ó¸ê®Æªº«¬ºA¡C

¤j³¡¥÷µ{¦¡»y¨¥¨Ò¦p Pascal¡BC++¡B Java µ¥¡A Åܼƪº«¬ºA¬O¤£¥i¥HÀH®É§ïÅܪº¡A ¨Ò¦p§A©w¸q¤F¤@­Ó¦r¦ê«¬ºAªºÅܼơA §A¥u¥i¥H¥Î¥¦¨Ó¦s¨ú¦r¦ê«¬ºAªº¸ê®Æ¡A ¦Ó¤£¥i¦s¨ú¨ä¥¦«¬ºAªº¸ê®Æ¡C

¦ý JavaScript ´N¤£¦P¤F¡A JavaScript ¬O¤@ºØ¼eÃP«¬ºA»y¨¥ (Loosely Typed Language)¡A §Y¬O»¡Åܼƪº«¬ºA¬O¥i¥HÀH®É§ïÅܪº¡C

¦n³B¡G
¤£¶·©w¸q¤Ó¦hÅܼơF ¤£¶·¨c°O¨º¤@­ÓÅܼƬO¨º¤@ºØ«¬ºA¡F ¤£¶·¼g¤Ó¦hªF¦è¨ÓÂà´«¸ê®Æ«¬ºA¡A ¦]¬° JavaScript ·|¦Û°Ê§PÂ_§A·Q¥Îªº¸ê®Æ«¬ºA¡C

Ãa³B¡G
ÁöµM JavaScript ·|¦Û°Ê§PÂ_§A·Q¥Îªº¸ê®Æ«¬ºA¡A ¦ý¦³®É¥¼¥²¬O§A¤ß¥Ø¤¤·Q¥Îªº¨º¤@ºØ«¬ºA¡A ¨Ò¦p§A·Q°µ¾ã¼Æ«¬ºAªº¹Bºâ¡A ¥¦Óý°µ¤F¦r¦ê«¬ºAªº¹Bºâ¡C

Netscape ¬°¤F¤è«K JavaScript »P Java ·¾³q¡A ¦ô­p±N¨Ó JavaScript ªº¸ê®Æ«¬ºA·|ºÉ¶q»P Java ¬Û®e¡A ¦]¦¹§Ú«Øij¤j®aºÉ¶q¯d·N¤@­ÓÅܼƪº«¬ºA¡C

°}¦C (Array)
°}¦C¬O¥Î¨ÓÀx¦s¦h­Ó¸ê®Æ¡A ¦Ó¥B³o¨ÇÅܼƬO¦P¤@«¬ºAªº ¡]¦ý JavaScript ¤¹³\¤@­Ó°}¦CÀx¦s¤£¦P«¬ºAªº¸ê®Æ¡^¡C °}¦C¤ºªº¸ê®Æ´NºÙ¬°¸Ó°}¦Cªº¤¸¯À (elements)¡A °}¦C¤ºªº¸ê®Æ¼Æ¥Ø´NºÙ¬°¸Ó°}¦Cªºªø«× (length)¡C

·í§A·Q¦s¨ú«Ü¦h¸ê®Æ¡A ´N¥i¥H¥Î°}¦C¡A ¦n³B´N¬O¤£¥Î¬°¨C­Ó¸ê®Æ³£©w¸q¤@­ÓÅܼƦWºÙ¡A ÁÙ¥i¥H¤è«K¦s¨ú¤j¶q¸ê®Æ¡C

¼gªk¡G©w¸q¤@­Ó°}¦C

¥u©w¸q¤@­Ó°}¦Cªº¦WºÙ¡A ¦ý¤£«ü©w¥¦ªº¤º®e¡G
var array_name = new Array() ;
©w¸q¤@­Ó°}¦Cªº¦WºÙ©Mªø«×¡A ¦ý¤£«ü©w¥¦ªº¤º®e¡G
var array_name = new Array(length) ;

©w¸q¤@­Ó°}¦Cªº¦WºÙ¡Bªø«×©M¤º®e¡G
var array_name = new Array(data1 , data2 , data3 , ... , dataN) ;

array_name ¬O°}¦Cªº¦WºÙ¡C
new ¬O¥Î¨Ó±N°O¾ÐÅé¤À°tµ¹¤@­Ó·sªºª«¥ó¡A ¥¦¬O¤@­ÓÃöÁä¦r¡C
Array ¬O¥Î¨Ó©w¸q°}¦Cªº¡C
length ¬O°}¦Cªº¸ê®Æ¼Æ¥Ø¡A ¥¦ªº­È¥i¥H¬O¥Ñ 1 ¶}©lªº¥¿¾ã¼Æ¡A ¨Ò¦p§A·Q©w¸q¤@­ÓÀx¦s¬P´Áªº­^¤å¦r°}¦C¡A length ªº­È´N¥i¥H¬O 7¡C
data1 , data2 , data3 , ... , dataN¡@¬O N ­Ó¸ê®Æ¡A ¨º»ò³o­Ó°}¦Cªºªø«×´N¬O N¡@ ¤F¡A ¨Ò¤l¡G

var week_array = new Array("Monday", "Tuesday", "Wednesday",
¡@¡@¡@ "Thursday", "Friday", "Saturday", "Sunday");

¨Ò¤l¤¤©w¸q¤F¤@­Ó°}¦C¨ÓÀx¦s¬P´Áªº­^¤å¦r¡A ³o­Ó°}¦C¦³ 7 ­Ó¸ê®Æ¡A ¦]¦¹°}¦Cªºªø«×´N¬O 7 ¤F¡C ¦]¬°¥y¤l¤Óªø¡A ©Ò¥H¼g¦¨¨â¦æ¡A ¦Ó¤À¹j¦ì¸m¥i¥H¦b³r¸¹ªº¥ª¥k¡C

©w¸q¤F¤@­Ó°}¦C«á¡A ´N¥i¥H¦s¨ú°}¦C¤ºªº¸ê®Æ¡C ­n¦s¨ú³o¨Ç¸ê®Æ¡A ´N­n¾Ç²ß¥¦­Ìªºªí¥Ü¤è¦¡¡C

ªí¥Ü¤è¦¡¡G°}¦C¤ºªº¸ê®Æ

array_name[index]

index ¬O°}¦C¤º¬Y­Ó¸ê®Æªº¦ì¸m¡A ¥¦¬O¥Ñ 0 ¶}©lªº¡A ¦Ó¤£¬O¥Ñ 1 ¶}©lªº¡A ¤j®a¶·­n¯d·N³oÂI¡A ¦]¬°³o­Ó¹q¸£·§©À»P¤HÃþ¹ê»Ú¥Í¬¡¤£¦P¡A ©Ò¥Hªì¾ÇªÌ«Ü©ö·|§Ë¿ùªº¡C ¦Ó index ªº³Ì¤j­È´N¬O length - 1 ¡A ¦Ó¤£¬O length ¡C
[¡@©M¡@]¡@ ¬O¥ª¤¤¬A¸¹©M¥k¤¤¬A¸¹¡A ¥Î¨Ó¬AµÛ index ¡C
§Ú­Ì¤]¥i¥H¨ú±o¤@­Ó°}¦Cªºªø«×¡A ´N¬O¥Î¤@­Ó°}¦Cªº length ÄÝ©Ê¡A ¦Ó¥B¥¦¬O¾ã¼Æ«¬ºAªº¡C

ªí¥Ü¤è¦¡¡G°}¦Cªºªø«×

array_name.length

§Ú­Ì´N¥Î¤W­±©w¸q¤Fªº week_array °µ¨Ò¤l¡A ¤U­±¦C¥X¤F week_array ¤º©Ò¦³¸ê®Æªºªí¥Ü¤è¦¡©M¥¦­Ìªº¤º®e¡G

ªí¥Ü¤è¦¡ ¤º®e
week_array[0] "Monday"
week_array[1] "Tuesday"
week_array[2] "Wednesday"
week_array[3] "Thursday"
week_array[4] "Friday"
week_array[5] "Saturday"
week_array[6] "Sunday"
week_array.length 7

¨Ò¤l¡G À³¥Î°}¦C




[/code]

°õ¦æ (2)

¨Ò¤l»¡©ú¡G

var student_name = new Array("Tom", "John", "Sam");
©w¸q¤@­Ó°}¦C¡A ¦WºÙ¬O student_name ¡A ¦P®É«ü©w¥¦ªº¤º®e¡A ¤À§O¬O "Tom", "John", "Sam" ¤T­Ó¦r¦ê«¬ºAªº¸ê®Æ¡C ºò°O²Ä¤@­Ó¸ê®Æªº¦ì¸m¬O 0¡A ¦Ó³oùئ³ 3 ­Ó¸ê®Æ¡A ©Ò¥H³Ì«á¤@­Ó¸ê®Æªº¦ì¸m´N¬O 2 ¤F¡C

var num_of_student = student_name.length;

©w¸q¤@­ÓÅܼơA ¦WºÙ¬O num_of_student ¡A µM«á±N«ü©w¥¦ªº¤º®e¡A ¤º®e¬O student_name.length ¡A ¦]¬° student_name °}¦C¤º¦³ 3 ­Ó¸ê®Æ¡A ©Ò¥H student_name.length ªº­È´N¬O 3 ¤F¡A ¦Ó¥B¥¦¬O¾ã¼Æ«¬ºA¡A ¦]¦¹ num_of_student ¬O¾ã¼Æ«¬ºAªºÅܼơC

var student_age = new Array(num_of_student);
©w¸q¤@­Ó°}¦C¡A ¦WºÙ¬O student_age ¡A ¦P®É«ü©w¥¦ªºªø«×¡A ªø«×¬° num_of_student ªº¤º®e¡A ¥ç§Y¬O 3 ¤F¡A ©Ò¥H student_age ´N¬O¤@­Ó¦³ 3 ­Ó¸ê®Æªº°}¦C¡C

student_age[0] = 16;
student_age[1] = 18;
student_age[2] = 20;
±N student_age °}¦C¤¤ªº²Ä¤@­Ó¸ê®Æªº¤º®e«ü©w¬° 16 ¡A ²Ä¤G­Ó¸ê®Æ«ü©w¬° 18 ¡A ²Ä¤T­Ó¸ê®Æ«ü©w¬° 20 ¡C


§Úªº js µ§°O»`¶°
§Úªº js µ§°O»`¶°
§Úªº js µ§°O»`¶°
§Úªº js µ§°O»`¶°
§Úªº js µ§°O»`¶°
§Úªº js µ§°O»`¶°
§Úªº js µ§°O»`¶°
¦^ ¡P §Úªº js µ§°O»`¶° ³o¤@½g¤å³¹«Ê­±


¥»¤å±i¶KªÌ¡G®Õ®ÕÂF¡e±i¶K®É¶¡¡G¥Á°ê100¦~5¤ë26¤é¡]¬P´Á¥|¡^14ÂI06¤À | §ó·s¦¸¼Æ #2 | ³Ì«á§ó·s¡G¥Á°ê100¦~6¤ë8¤é¡]¬P´Á¤T¡^16ÂI05¤À¡f | ¼g«Hµ¹®Õ®ÕÂF

³¡¸¨®æ­º­¶


¾Ç²ßªº¬G¶m­º­¶
¥»¯¸¤½§i¡G¡e±z¶V»Ý­n§Ú­Ì¡A§Ú­Ì´N¶V¦³³Ð·N¡f ¥»¯¸»¡©ú®Ñ¡G¡eµo²{¬G¶mÁÙ¦³§ï¶iªº¦a¤è¡A½Ð¨Ó«H§i¶D­ì¤B­Ì¡f
Æ[¹îÀ³¥Î¾Ç²ßÂI¼Æ ¡G¡e«¥ªº¬G¶m¦³±zªº°Ñ»P¡A·|¨Ï§Ú­Ì¦³§ó¤jªºµo´§ªÅ¶¡¡A®i²{§óÂ×´Iºë±mªº¾Ç²ßµe­±¡f ¡e´Á«ÝÂǥѵL»Ùêºô­¶³]­p¡A¯àÅýµø»Ù¤pªB¤Í§ó·R¬Ý®Ñ¡B§ó·R¼g§@¥B§ó·R¾Ç²ß¡f¡Gª¼¥Î¹q¸£¡u¤ß±o¤À¨É¡v
¡e¬°¤FÅý§Ú­Ì¦³°®²bªº¾Ç²ßÀô¹Ò¡A½Ð¤Å¥ô·N¦b¥»¯¸´²¼½°Ó·~¼s§i»P¤£¦Xªk¤å¥ó©ÎÁpµ²¡f¡G¥»¯¸«Å¥Ü