8.函式引數的預設值
阿新 • • 發佈:2022-01-02
<!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>
// 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>