ES6带标签的模板字符串

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.

你可能感兴趣的:(ES6,ES7,ES8,javascript,es6)