一、定义:
template string:模版字符串,使用反引号`开头和结尾,中间可以包含占位符,占位符语法${}。
二、 基本用法:
1. var x = 1 ;
var y = 2 ;
`${x}` + `${y}` //'12'
2. var [elem1,elem2] = ['hello','world'];
`this is first demo: ${elem1},${elem2} `;
3. var {name,age} = {name:'andy',age:18} ;
`${name}'s age is ${age}`;
三、 预处理
模版字符串支持预处理,格式为 prefunc + template string;
其中prefunc接受参数的形式为: [string], 占位符字段返回值1 , 占位符字段返回值2
function prefunc(string){
console.info(arguments);
}
var name = 'andy'
, age = '100';
prefunc `hello ${name}, the world age is ,${age}亿年`;
// [["hello ", ", the world age is ,","亿年"],"andy","100"]
1.优点:
能内嵌简单表达式
2.缺点:
不支持带逻辑的内嵌表达式
refer:
1. http://www.tuicool.com/articles/yAfq6bq
2.http://www.cnblogs.com/ziyunfei/archive/2012/12/12/2814219.html