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>
posted @ 2021-06-21 11:40  胡勇健  阅读(38)  评论(0)    收藏  举报