模板字符串 提供构造字符串的语法糖,在 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);
本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:http://www.cnblogs.com/starof/p/6919746.html有问题欢迎与我讨论,共同进步。