JavaScript(四)---- 函數

來源:互聯網
上載者:User

標籤:函數重載   類型   同名   重載   使用   efault   show   tle   argument   

函數主要用來封裝具體的功能代碼。

函數是由這樣的方式進行聲明的:關鍵字 function、函數名、一組參數,以及置於括弧中的待執行代碼。

格式:

  function 函數名(形參列表){
        函數體 ;    
    }

javascript的函數要注意的細節:
    1. 在 javascript中函數定義形參時是不能使用var關鍵字聲明變數的,直接寫參數名即可。
    2. 在javascript中的函數是沒有傳回值類型 的,如果函數需要返回資料給調用者,直接返回即可,如果不需要返回則不返回。
    3. 在 javascript中是沒有函數重載的概念的,後定義的同名函數會直接覆蓋前面定義同名函數。
    4. 在javascript中任何的函數內部都隱式的維護了一個arguments(數組)的對象,給函數 傳遞資料的時候,是會先傳遞到arguments對象中,
    然後再由arguments對象分配資料給形參的。

程式碼範例

 1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <script> 4  5     function showDay(){ 6         //找到對應 的標籤對象。 7         var inputObj = document.getElementById("month"); 8         //擷取input標籤資料 9         var month = inputObj.value;10         /*11         if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){12             alert("本月是31天");    13         }else if(month==4||month==6||month==9||month==11){14             alert("本月是30天");    15         }else if(month==2){16             alert("本月是28天");    17         }else{18             alert("沒有該月份");    19         }20         */21         22         month = parseInt(month);23         switch(month){24             case 1:25             case 3:26             case 5:27             case 7:28             case 8:29             case 10:30             case 12:31                 alert("本月是31天");32                 break;33             case 4:34             case 6:35             case 9:36             case 11:37                 alert("本月是30天");38                 break;39             case 2:40                 alert("本月是28天");41                 break;42             default:43                 alert("沒有該月份");44                 break;45         46         }47         48         49     }50 51 52 </script>53 54 55 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />56 <title>無標題文檔</title>57 </head>58 <body>59     月份:<input id="month" type="text" /><input type="button" value="查詢" onclick="showDay()" />60 61 </body>62 </html>

注意:從網頁中擷取到的資料都是string類型。

JavaScript(四)---- 函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.