¡u¨ç¼Æ¡v¡]Functions¡^¬O©Ò¦³ªºµ{¦¡»y¨¥¥²³Æªº¥\¯à¡CJavaScript ªº¨ç¼Æ¥i¥H¤À¦¨¤U¦C´XÃþ¡G¥»¤p¸`±N°w¹ï¨Ï¥ÎªÌ©w¸qªº¨ç¼Æ¨Ó¶i¦æ»¡©ú¡C
- ¤º«Ø¨ç¼Æ¡G¤S¤À¦¨¨âÃþ
- ¤@¯ë¤º«Ø¨ç¼Æ¡A¦p parseInt() ©M parseFloat() µ¥¡C
- »Pª«¥óµ²¦Xªº¨ç¼Æ¡A¨Ò¦p Math.sin() ©Î string.big()µ¥¡A³oÃþªº¨ç¼Æ¤SºÙ¬°ª«¥óªº¤èªk¡]Method¡^¡C
- ¨Ï¥ÎªÌ©w¸qªº¨ç¼Æ¡G¤]¥i¥H¤À¤@¯ë¨ç¼Æ»Pª«¥óªº¤èªk¨âÃþ¡C
¨ç¼Æªº©w¸q¥]§t¨ç¼Æ¦WºÙ (Function name) ¤Î¿é¤J¤Þ¼Æ (Input arguments)¡A¨ä°ò¥»®æ¦¡¦p¤U¡G
«ÂI»¡©ú¦p¤U¡Gfunction functionName(InputArguments) { JavaScript statements... ... return(output) // ¥i¬Ù²¤ } ¦b¥H¤U½d¨Ò¤¤¡A§Ų́ϥΨâÓ¨ç¼Æ¨ÓÅã¥Ü²{¦b®É¶¡»P¬P´Á´X¡G
- ¦b¬A¸¹¸Ìªº¿é¤J¤Þ¼Æ¡]Input Arguments¡^¡A¥²¶·¥H³r¸¹¤À¶}¡C
- ©w¸q¨ç¼Æ¨Ã¤£¥Nªí¨ç¼Æªº°õ¦æ¡A¥u¦³¦bµ{¦¡¤¤©I¥s¨ç¼Æªº¦WºÙ«á¡A¤~·|°õ¦æ¸Ó¨ç¼Æ¡C
- Y¦³»Ýn¡A¨ç¼Æ³Ì«á¥i¥Î return ¨Ó¶Ç¦^¿é¥XÅܼơ]¼ÆÈ¡B¦r¦ê¡A©Î¨ä¥L«¬ºAªºÅܼơ^¦Ü©I¥s¦¹¨ç¼Æªºµ{¦¡Àô¹Ò¡C
- ¤@¯ëºô¶¤¤ªºªº¨Æ¥ó³B²z¾¹¡]Event handlers¡^¡A³q±`³£¬O¥H JavaScript ©Î¬O VBScript ªº¨ç¼Æ¨Ó´yz¡C
- ¨ç¼Æªº©w¸q¡A³q±`¼g¦b <head> ¤Î </head> ¤§¶¡¡A¬Æ¦Ü¤]¥i¥H¼g¦bºô¶³Ì«e±¡]¦b <html> ¤§«e¡^¡A¥H½T«O HTML ¥DÅé¦b³Q§e²{«e¡A©Ò¦³¬ÛÃöªº JavaScript ¨ç¼Æ³£¤w³Q¸ü¤J¡A¨ÃÀH®É¥i³Q°õ¦æ¡A¦ý³o¨Ã¤£¬O¤@ÓÄY®æªº³W©w¡C
- ¤@¯ë¨Ó»¡¡A§Ú̧Ʊæ¨ç¼Æªº©w¸q¥X²{ªº¦ì¸m©M¥¦³Q©I¥s¤§³B¯à¶V±µªñ¶V¦n¡A¥H¤è«Kµ{¦¡ºÞ²z¡A¦b³oºØ±¡ªp¤U¡A¥un¨ç¼Æ©w¸q¥X²{¦b¨ä³Q©I¥s¤§«e¡A¤jP¤W³£¤£·|¦³¤°»ò°ÝÃD¡C
¦¹½d¨Òì©l½X¦p¤U¡G
¦b¨Ï¥Î¨ç¼Æ®É¡A§ÚÌ¥²¶·¦³¡u§½³¡Åܼơv©M¡u¥þ°ìÅܼơvªº·§©À¡A¥i²³æ©w¸q¦p¤U¡G
¤@¯ë¦Ó¨¥¡A¦pªG§Ú̥ΪºÅܼƥu¦b¤@Ó¨ç¼Æªº¦³®Ä½d³ò¡]Scope¡^¤º¹B§@¡A«h¦¹ÅܼƳq±`´NÀ³¸Ó³Q³]©w¬°§½³¡ÅܼơA¥H§K¦b¨ç¼Æ°õ¦æ«á²£¥Í°Æ§@¥Î¡A¼vÅT¨ì¨ä«áµ{¦¡½Xªº°õ¦æ¡C¨Ò¦p¡G
- §½³¡ÅÜ¼Æ (Local variables): ¥u¦³¦bÅܼƥ»¨ªº¨ç¼Æ¸Ì¤~¬Ý±o¨£ªºÅܼơC±ý©w¸q§½³¡ÅܼơA¥i¦bÅܼƲĤ@¦¸¨Ï¥Î®É¡A¥[¤W var¡C
- ¥þ°ìÅÜ¼Æ (Global variables): ¦b¾ãÓµ{¦¡³]pªº¹Lµ{¤¤³£¥i¥H¬Ý±o¨£¡B ¦Ó¥B¨C¤@Ó¨ç¼Æ³£¥i¥H¥ÎªºÅܼơCY¤£¹ïÅܼưµ¥ô¦ó³B²z¡AJavaScript ªºÅܼƹw³]ª¬ºA§Y¬O¥þ°ìÅܼơC
¦¹½d¨Òªº§¹¾ãì©lÀɮצp¤U¡G
¦b¤Wz½d¨Ò¤¤¡Ax ©M y ³£¬O©w¸q¬°¥þ°ìÅܼơA¦ý¬O¦b¨ç¼Æ testScope() ¤º¡A¥t¤@Ó¦P¦Wªº°Ï°ìÅÜ¼Æ x ¾B½ª¤Fì¥ýªº¥þ°ìÅÜ¼Æ x¡A¦]¦¹¦b¨ç¼Æ¤º¦L¥X¨Óªº x ȬO 5¡C¦ý¬OÂ÷¶}¨ç¼Æ¤§«á¡Ax ÁÙ¬O¤@Ó¥þ°ìÅܼơA¨äÈÁÙ¬O 10¡CÅÜ¼Æ y «h¬O¥þ°ìÅܼơA¦]¦¹Y¦b¨ç¼Æ¤¤§ïÅܨäÈ¡A¤]·|¤Ï¬M¨ì¨ç¼Æ¥~ªº y¡C
¤@Ó±`µo¥Íªº¿ù»~¡A´N¬O¨S¦³§â¨ç¼Æ¤º³¡ªºÅܼƳ]©w¦¨§½³¡ÅܼơA¾ÉP¦¹ÅܼƷ|Ä~©Ó©Î¼vÅT¥~³¡ÅܼƪºÈ¡C¦]¦¹¡A¬°¤F´î¤Ö°£¿ùªº®É¶¡¡A©Ò¦³¨ç¼Æªº¤º³¡ÅܼơA¦b²Ä¤@¦¸¨Ï¥Î®É³Ì¦n¥[¤W var¡A¤w½T»{¨ä¦³®Ä½d³ò¥u¦b¦¹¨ç¼Æ¤º¡C
¦b¨ç¼Æ¥~³¡©w¸qªºÅܼơAµL½×¬O§_¦³¥[ var¡A³£·|³Qµø¬°¬O¥þ°ìÅܼơA¦]¦¹¥i¥H¤£¥²¨è·N¦A¥h¥[ var¡C
JavaScript µ{¦¡³]p»PÀ³¥Î¡G¥Î©óºô¶¥Î¤áºÝ