ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[±Ð¾Ç] (±Ð¾Ç²Ä8½g)JavaScript ¨ç¦¡

[±Ð¾Ç] (±Ð¾Ç²Ä8½g)JavaScript ¨ç¦¡

¨ç¦¡ªº¼¶¼g¥i¥H»¡¬OJavaScriptµ{¦¡ªº®Ö¤ß¡A©Ò¿×¨ç¦¡(function)´N¬O±N·|¸g±`¼¶¼g¦Ó­«½Æ°õ¦æªºµ{¦¡±Ô­z°Ï¶ô«Ê¸Ë°_¨Ó¦¨¬°¤@¿W¥ßªºµ{¦¡³æ¤¸¡A¥H«K¼W¶iµ{¦¡ªº°õ¦æ®Ä¯à»P¸`¬Ùµ{¦¡¶}µoªº®É¶¡¡C

¨ç¦¡¼¶¼g®æ¦¡
  1. function ¨ç¦¡¦WºÙ(°Ñ¼Æ1,°Ñ¼Æ2¡K)
  2. {
  3.   ¨ç¦¡µ{¦¡°Ï¶ô
  4. }
½Æ»s¥N½X

  • ­n¼¶¼g¨ç¦¡¥²¶·¨Ï¥Îfunction³o­Ó«O¯d¦r¨Ó«Å§i¨ç¦¡¡C
  • ¥²¶·«ü©w¨ç¦¡ªº¦WºÙ¡A­Y§Ú­Ì­n¨Ï¥Î¨ç¦¡¡A«h§Ú­Ì¥²¶·©I¥s¦¹¨ç¦¡¦WºÙ¡C
  • ¦b¨ç¦¡¡y¡]¡^¡z¬A¸¹¤ºµ¹¤©«ü©w°Ñ¼Æ¡A¦¹°Ñ¼Æ¬O§Ú­Ì©I¥s¨ç¦¡®É©Ò¶Ç»¼ªº°T®§¸ê®Æ¡A¦b¸Ó¨ç¦¡¤º¨Ï¥Î¸Ó°Ñ¼Æ´N¦p¦P¨Ï¥ÎÅܼƤ@¯ë¡C
  • ¦b¨ç¦¡¡y{}¡z¤j¬A¸¹¤º©Ò¼¶¼gªºµ{¦¡±Ô­z¥u¦b¨ç¦¡³Q©I¥s¨Ï¥Î®É¤è·|°õ¦æ¡C


©I¥s¨ç¦¡
°²³]§Ú­Ì¼¶¼g¤F¤@­Ó¨ç¦¡¦p¤U¡G
  1. function myfunction()
  2. { document.write(¡§Åwªï¥úÁ{!!¡¨) ;}
½Æ»s¥N½X
³o­Ó¨ç¦¡­Y³æ¯Âªº©ñ¸m¦bHTML¤å¥ó¤¤±N¬O²@µL·N¸qªº¡A¦]¬°¡G¨ç¦¡¨S¦³³Q©I¥s¨Ï¥Î¡A«h¨ç¦¡¤ºªºµ{¦¡±Ô­z¬O¤£·|³Q°õ¦æªº¡I¦pªG­n¨Ï¨ç¦¡¤ºªºµ{¦¡½X³Q°õ¦æ¡A«h§Ú­Ì¥²¶·©I¥s¸Ó¨ç¦¡¡G
  1. <HTML>
  2. <HEAD>
  3. <TITLE>©I¥s¨ç¦¡</TITLE>
  4. </HEAD>
  5. <BODY>

  6. <SCRIPT Language="JavaScript">
  7. function myfunction()
  8. {
  9. document.write("Åwªï¥úÁ{!!");
  10. }
  11. myfunction(); //©I¥s¨ç¦¡
  12. </SCRIPT>

  13. </BODY>
  14. </HTML>
½Æ»s¥N½X
¥Ñ¤W¨Ò§Ú­Ì¥i¥H²M·¡ªº¤F¸Ñ¡G­Y·Q­n°õ¦æ¬Y­Ó¨ç¦¡¡A¥u­nª½±µ©I¥s¨ä¦WºÙ§Y¥i¡I

±a¦³°Ñ¼Æªº¨ç¦¡
¦b¤W¤@¤p¸`ªºmyfunction()¨ç¦¡¤¤¡A§Ú­Ì¨Ã¨S¦³¨Ï¥Î¡y°Ñ¼Æ¡z¡A¤]´N»¡¡Gmyfunction()¨ç¦¡ªº¤º®e¡]¿é¥X°Ý­Ô»y¡GÅwªï¥úÁ{!!¡^¥Ã»·¬O©T©w¦Ó¤£·|Åܤƪº¡A¦]¬°§Ú­Ì¨Ã¨S¦³¤@­Ó¥O¨ç¦¡²£¥ÍÅܤƪº¡y°Ñ¼Æ(ÅܼÆ)¡z¡I  ¬°¤F¨Ï¨ç¦¡¯à¦³¦h¼Ë¤Æªº®Ä¯à¡A©Ò¥H§Ú­Ì¥²¶·µ½¥[§Q¥Î¨ç¦¡ªº¡y°Ñ¼Æ¡z¡G
  1. <HTML>
  2. <HEAD>
  3. <TITLE>§t¦³°Ñ¼Æªº¨ç¦¡</TITLE>
  4. </HEAD>
  5. <BODY>

  6. <SCRIPT Language="JavaScript">
  7. function myfunction(msg)
  8. {
  9. document.write(msg);
  10. }
  11. myfunction("Åwªï¥úÁ{!!"); //©I¥s¨ç¦¡
  12. myfunction("¿Ë·Rªººô¤Í"); //©I¥s¨ç¦¡
  13. </SCRIPT>

  14. </BODY>
  15. </HTML>
½Æ»s¥N½X
¦b¥»¨Ò¤¤¡G

  • §Ú­Ì¬°¨ç¦¡«ü©w¤F¤@­Ó°Ñ¼Æmsg¡A¦]¦¹¦b¨ç¦¡ªºµ{¦¡±Ô­z¤¤±N¤£·|¬O¿é¥X¤@¬q©T©wªº¦r¦ê¡A¿é¥Xªº¸ê®Æ¤º®e±NÀH°Ñ¼Æmsg¦Ó§ïÅÜ¡C
  • µ{¦¡½X²Ä12¦æ¡A²Ä¤@¦¸©I¥s¨ç¦¡®É§Ú­Ì«ü©w¤F¤@­Ó°Ñ¼Æ­È¡yÅwªï¥úÁ{!!¡zµ¹¨ç¦¡ªº°Ñ¼Æmsg¡A¦¹®É¨ç¦¡¤ºªºµ{¦¡±Ô­zÀ°§Ú­Ì¿é¥X¤F¡yÅwªï¥úÁ{!!¡z³o¬q¦r¦ê¡C
  • µ{¦¡½X²Ä13¦æ¡A¦A¦¸©I¥s¨ç¦¡¡A¦¹®É§Ú­Ì«ü©w¤F¥t¤@­Ó°Ñ¼Æ­È¡y¿Ë·Rªººô¤Í¡zµ¹¨ç¦¡ªº°Ñ¼Æmsg¡A¦¹®É¨ç¦¡¤ºªºµ{¦¡±Ô­zÀ°§Ú­Ì¿é¥X¤F¡y¿Ë·Rªººô¤Í¡z³o¬q¦r¦ê¡A¬Û¦Pªº¸ê®Æ¿é¥X°Ê§@¡A¦ý¬O¿é¥Xªºµ²ªG«o¬O¤£¦Pªº¡C


¨ç¦¡ªº¦^¶Ç­È
°Ñ¼Æªº¶Ç»¼¨Ã«D¬O³æ¦Vªº¡A§Ú­Ì¥i¥H¦b©I¥s¨ç¦¡®É¶Ç»¼¸ê®Æµ¹¨ç¦¡¡A¬Û¹ïªº¡A¨ç¦¡¤]¥i¥H¶Ç»¼¸ê®Æµ¹¨ç¦¡¥»¨­¥H¥~ªºµ{¦¡©Î¨ç¦¡¡A«ç»òÅý¨ç¼Æ¦³¦^¶Ç­È©O¡H«Ü²³æ¡A¥u­n¦b¨ç¼Æµ²§ô¤§«e¡A­È¦æ¤U¦C®æ¦¡ªº±Ô­z§Y¥i¡C
  1. ¨ç¦¡¦W = ±ý¶Ç»¼ªº¦^¶Ç­È
½Æ»s¥N½X
¨Ò¦p¡G§Ú­Ì¼¶¼g¤@­Óf(x)=x*2+xªº¨ç¦¡¡C
  1. function f(x)
  2. {
  3. f = x*2 + x;
  4. }
½Æ»s¥N½X
¦b¤W¨Ò¤l¤¤¨ç¦¡ªº¦WºÙ¬°f¡A¦P®É§t¦³¤@­Ó°Ñ¼Æx¡A¦]¬°§Ú­Ì§Æ±æ¨ç¦¡¯à¶Ç¦^¹Bºâ¦¡ªººâµ²ªG¡A©Ò¥Hª½±µ±N¡yx^2+x¡zªº¹Bºâµ²ªG«ü©wµ¹¨ç¼Æ¦WºÙf¡I

Åܼƪº«Å§i
¦b¤§«eªº¾Ç²ßÀ³¥Î¤¤¡A·í§Ú­Ì­n¨Ï¥ÎÅܼƮɳ£¬Oª½±µ®³¨Ó´N¥Î¡A¨Ò¦p¡G
  1. x = ¡§½m²ßÃD¡¨;
½Æ»s¥N½X
¨ä¹ê¡A³o¬O§Ú­Ì²²¤¤F¡y«Å§i¡zªº°Ê§@¡A¹ê»Ú¤W¡A¤Z¬O­n¨Ï¥ÎÅܼƴN¥²¶·¥ý¶i¦æÅܼƪº«Å§i¡A¥¿½TªºÅܼƨϥΤ覡¦p¤U¡G
  1. var x;
  2. x = ¡§½m²ßÃD¡¨;
½Æ»s¥N½X
¤W¦C±Ô­z¤¤ªº¡yvar¡z§Y¬°¡y«Å§i¡zªº·N«ä¡A¦pªG§Ú­Ì¨S¦³¥ý¦æ«Å§iÅܼƴNª½±µ¨Ï¥Î¡A¨º§Ú­Ì©Ò¨Ï¥Îªº³o­ÓÅܼơAJavaScript´Nª½±µ±N¥¦ÂkÃþ¬°¡y¥þ°ì(Global)¡zÅܼơI¥þ°ìÅܼƬO¥ô¦óÄÝ©óµ{¦¡ªºª«¥ó³£¥i¥H§Q¥Îªº¡A¬Û¹ï©ó¥þ°ìÅܼÆÁÙ¦³¥t¤@ºØÅܼÆÃþ«¬ºÙ¬°¡G¡y°Ï°ì(Local)Åܼơz¡A°Ï°ìÅܼƪº¼vÅT½d³ò´N¤ñ¸û¤p¡A¥u­­©ó«Å§iÅܼƪº¨ç¦¡¤º¦³®Ä¡A·íÂ÷¶}µ{¦¡ªº°õ¦æ¨ç¦¡ªð¦^¥Dµ{¦¡®É¡A°Ï°ìÅܼƴN·|±q°O¾ÐÅ餤³QÄÀ©ñ±¼¡A¨º§Ú­Ì¸Ó¦p¦ó«Å§i¥þ°ì¤Î°Ï°ìÅܼƩO¡H¨äÃöÁä´N¦b©óÅܼƫŧiªº¦ì¸m¡I
¨Ò¦p¡G
  1. <HTML>
  2. <HEAD>
  3. <TITLE>¥þ°ìÅܼÆ</TITLE>
  4. </HEAD>
  5. <BODY>

  6. <SCRIPT Language="JavaScript">
  7. test=5;
  8. myfunction(10);
  9. document.write(test);
  10. function myfunction(num)
  11. {
  12. test=num*2+num;
  13. }
  14. </SCRIPT>

  15. </BODY>
  16. </HTML>
½Æ»s¥N½X
½Ð°Ý¡G¤W¨Ò¤¤³Ì«áªºÅܼÆtest¿é¥X­È¬°¦h¤Ö¡A·íµM¤£·|¬O¡y5¡z¤F¡I¦]¬°testÅܼƦb¦¹¬°¥þ°ìÅܼƥô¦óÄÝ©óµ{¦¡ªºª«¥ó³£¥i¥H§Q¥Îªº¡A¦]¦¹¡A©I¥s¨ç¦¡«á¡AÅܼÆtest´N³Q«ü©w¬°¨ç¦¡¹Bºâµ²ªG¡u30¡v¡A¦Ó¤£¬O­ì¨Óªº¡u5¡v¤F¡I¨º«ç»ò¿ì©O¡H§Ú­Ì¥i¥H±Nµ{¦¡§ï¦¨³o¼Ë¡G
  1. <HTML>
  2. <HEAD>
  3. <TITLE>°Ï°ìÅܼÆ</TITLE>
  4. </HEAD>
  5. <BODY>

  6. <SCRIPT Language="JavaScript">
  7. test=5;
  8. myfunction(10);
  9. document.write(test);
  10. function myfunction(num)
  11. {
  12. var test;
  13. test=num*2+num;
  14. }
  15. </SCRIPT>

  16. </BODY>
  17. </HTML>
½Æ»s¥N½X
¦p¦¹¤@¨Ó¡AtestÅܼƭȴN¨ÌµMºû«ù¬°¡u5¡v¡A³o¥Nªí§Ú­Ì¦b¨ç¦¡¤º©Ò«Å§iªºtestÅܼƸò¨ç¦¡¤§¥~ªºÅܼÆtest¤@ÂIÃö«Y³£¨S¦³¡I¤]´N¬O»¡ÁöµMÅܼƦWºÙ³£¬O¡utest¡v¡A¦ý¬O¦b¨ç¦¡¤º©Ò¨Ï¥Îªº¡utest¡v¬O­Ó°Ï°ìÅܼơA¦Ó¨ç¦¡¥~©Ò¨Ï¥Îªº¡utest¡v«h¬O¤@­Ó¥þ°ìÅܼơI

´£¥Ü¡G
·í§Ú­Ì¦b¨ç¦¡¤¤¨Ï¥ÎÅܼƮɡA½Ð¾¨¶q¨Ï¥Îvar¨Ó«Å§iÅܼơA¥H¥²ÁקK¨ç¦¡¦b°õ¦æ¹Lµ{¤¤·N¥~ªº§ïÅܤF¥þ°ìÅܼƦӳy¦¨¾ãÅéµ{¦¡°õ¦æªº¿ù»~¡I
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

        ÀR«ä¦Û¦b : §Ú­Ì­n°µ¦nªÀ·|ªºÀô«O¡A¤]­n°µ¦n¤º¤ßªºÀô«O¡C
ªð¦^¦Cªí ¤W¤@¥DÃD