8.函数参数的默认值

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // ES6允许给函数参数默认初始值
      // 1.形参默认值,具有默认值的参数,一般位置要靠后
      // function add(a = 3, b = 5, c = 7) {
      //   return a + b + c;
      // }

      // let result = add();
      // console.log(result);

      // 2.形参默认值可以与解构赋值结合使用
      function connect({ host = "127.0.0.1", username, password, port }) {
        console.log(host);
        console.log(username);
        console.log(password);
        console.log(port);
      }

      connect({
        host: "localhost",
        username: "root",
        password: "root",
        port: 3306,
      });
    </script>
  </body>
</html>
posted @ 2022-01-02 16:02  问某完红  阅读(27)  评论(0)    收藏  举报