es6 函数参数默认值
函数参数默认值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>函数参数默认值</title>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div id="ad">
</div>
<script>
//1 形参初始值
function add(a, b = 3){
return a + b;
}
let result = add(3);
console.log(result);
//2 与解构赋值结合
function connect({host='127.0.0.1',username,password,port=3306}) {
console.log(host);
console.log(username);
console.log(password);
console.log(port);
}
connect({
username: 'root',
password:'123456'
})
</script>
</body>
</html>

浙公网安备 33010602011771号