开发者社区> 问答> 正文

模板字符串

模板字符串

展开
收起
茶什i 2019-11-22 16:48:31 993 0
1 条回答
写回答
取消 提交回答
  • 就是这种形式${varible},在以往的时候我们在连接字符串和变量的时候需要使用这种方式'string' + varible + 'string'但是有了模版语言后我们可以使用string${varible}string 这种进行连接。基本用途有如下: 1、基本的字符串格式化,将表达式嵌入字符串中进行拼接,用${}来界定。

    //es5
    var name = "lux";
    console.log("hello" + name);
    //es6
    const name = "lux";
    console.log(`hello ${name}`); //hello lux
    

    2、在 ES5 时我们通过反斜杠()来做多行字符串或者字符串一行行拼接,ES6 反引号(``)直接搞定。

    //ES5
    var template =
      "hello \
    world";
    console.log(template); //hello world
    
    //ES6
    const template = `hello
    world`;
    console.log(template); //hello 空行 world
    
    2019-11-22 16:49:09
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载