重學(xué)ES系列之模版字符串

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模版字符串</title>
</head>
<body>
    
</body>
<script>
    let name = "maomin";
    let age = "20";
    console.log("user="+name+"  "+"age="+age);
    let str = `name=${name}, age=${age}`;
    console.log(str);
    // 在模版字符串內(nèi)可以進(jìn)行調(diào)用。
    function showName(params) {
        return "I am maomin"
    }
    console.log(`${showName()}`);
    // 如果通過(guò)模版字符串進(jìn)行傳參的話(huà),第一個(gè)參數(shù)是一個(gè)數(shù)組,是除$以外的內(nèi)容。
    function add(a,b,c) {
        return a[0]+b+a[1]+c
    }
    let out = add`name=${name},age=${age}`;
    console.log(out);
    // 也可以這樣
    function add1(name,age) {
        return add`name=${name},age=${age}`
    }
    console.log(add1("xqm","20"))
</script>
</html>

作者:Vam的金豆之路

主要領(lǐng)域:前端開(kāi)發(fā)

我的微信:maomin9761

微信公眾號(hào):前端歷劫之路