zoukankan      html  css  js  c++  java
  • es6 模板字符串

    模板字符串 提供构造字符串的语法糖,在 Prel/python 等语言中也都有类似特性。

    1、反引号模板,可以换行

    2、反引号模板,可以嵌套

    用+``来嵌套

    好处:语法更加简洁

            var name="123456一堆返回的数据"
            var desc="title内容";
            var tpl=`
            <div>
                <span>${name}</span>
            </div>
            `;
            console.log(tpl);
            /*<div>
            <span>123456一堆返回的数据</span>
            </div>*/
            var tpl2=`
            <div>
            <span>${name +`<strong>${desc}</strong>`}</span>
            </div>
            `;
            console.log(tpl2);
            /*<div>
             <span>123456一堆返回的数据<strong>title内容</strong></span>
             </div>*/

    其它例子

    // Basic literal string creation
    `This is a pretty little template string.`
    
    // Multiline strings
    `In ES5 this is
     not legal.`
    
    // Interpolate variable bindings
    var name = "Bob", time = "today";
    `Hello ${name}, how are you ${time}?`
    
    // Unescaped template strings
    String.raw`In ES5 "
    " is a line-feed.`
    
    // Construct an HTTP request prefix is used to interpret the replacements and construction
    GET`http://foo.org/bar?a=${a}&b=${b}
        Content-Type: application/json
        X-Credentials: ${credentials}
        { "foo": ${foo},
          "bar": ${bar}}`(myOnReadyStateChangeHandler);
    View Code

    本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:http://www.cnblogs.com/starof/p/6919746.html有问题欢迎与我讨论,共同进步。

  • 相关阅读:
    作业20181127-1 附加作业 软件工程原则的应用实例分析
    20181120-1 每周例行报告
    20181113-2 每周例行报告
    获奖感言
    作业 20181030-4 每周例行报告
    20181023-3 每周例行报告
    Weekly 13
    Weekly 10
    Weekly 11
    weekly 8
  • 原文地址:https://www.cnblogs.com/starof/p/6919746.html
Copyright © 2011-2022 走看看