在 JavaScript 中使用可變函式引數的最佳方式是什麼?


如需在 JavaScript 中包含可變函式引數,你可以嘗試以下方法。

function myFunc(a, b = 0) {
   // function body
}

示例

你可以嘗試執行以下程式碼,以瞭解如何包含可變函式引數 −

<html>
   <body>
      <script>
         // default is set to 1
         function inc(val1, inc = 1) {
            return val1 + inc;
         }
         
         document.write(inc(10,10));
         document.write("<br>");
         document.write(inc(10));
      </script>
   </body>
</html>

更新日期: 2020 年 6 月 23 日

188 瀏覽

啟動你的 職業生涯

完成課程即可獲得認證

開始
宣傳
© . All rights reserved.