模板字符串 提供构造字符串的语法糖,在 Prel/python 等语言中也都有类似特性。
1、反引号模板,可以换行
2、反引号模板,可以嵌套
用+``来嵌套
好处:语法更加简洁
var name="123456一堆返回的数据" var desc="title内容"; var tpl=`${name}`; console.log(tpl); /*123456一堆返回的数据*/ var tpl2=`${name +`${desc}`}`; console.log(tpl2); /*123456一堆返回的数据title内容*/
其它例子
// Basic literal string creation`This is a pretty little template string.`// Multiline strings`In ES5 this is not legal.`// Interpolate variable bindingsvar name = "Bob", time = "today";`Hello ${name}, how are you ${time}?`// Unescaped template stringsString.raw`In ES5 "\n" is a line-feed.`// Construct an HTTP request prefix is used to interpret the replacements and constructionGET`http://foo.org/bar?a=${a}&b=${b} Content-Type: application/json X-Credentials: ${credentials} { "foo": ${foo}, "bar": ${bar}}`(myOnReadyStateChangeHandler);
本文作者,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:有问题欢迎与我讨论,共同进步。