1. // 模板字符串
    2. // 反引号包裹
    3. // const str = `hello es2015, this is a string`
    4. // 允许换行
    5. // const str = `hello es2015,
    6. // this is a \`string\``
    7. // console.log(str)
    8. const name = 'tom'
    9. // 可以通过 ${} 插入表达式,表达式的执行结果将会输出到对应位置
    10. const msg = `hey, ${name} --- ${1 + 2} ---- ${Math.random()}`
    11. console.log(msg)

    带标签的模板字符串

    1. // 模板字符串的标签就是一个特殊的函数,
    2. // 使用这个标签就是调用这个函数
    3. // const str = console.log`hello world`
    4. const name = 'tom'
    5. const gender = false
    6. function myTagFunc (strings, name, gender) {
    7. // console.log(strings, name, gender)
    8. // return '123'
    9. const sex = gender ? 'man' : 'woman'
    10. return strings[0] + name + strings[1] + sex + strings[2]
    11. }
    12. const result = myTagFunc`hey, ${name} is a ${gender}.`
    13. console.log(result)