const str = console.log`hello`; // [ 'hello' ]
上述代码中console.log相当于是模板字符串的标签。
应用:
const name = 'zs';
const gender = true;
function myTagFun(strings, name, gender) {
console.log(strings); // [ 'hello, ', ' is a ', '.' ]
const sex = gender ? 'man' : 'woman';
return strings[0] + name + strings[1] + sex + strings[2];
}
const str = myTagFun`hello, ${name} is a ${gender}.`;
console.log(str); // hello, zs is a man.