Trying to use Tagged template strings gives 'Uncaught SyntaxError: Unexpected token'

Trying to use Tagged template strings gives 'Uncaught SyntaxError: Unexpected token'

我在以下代码中使用标记的模板字符串

var a = 5;
var b = 10;
var pp="";
function tag(strings, ...values) {
  pp+=strings[0]; // "Hello "
  pp+=strings[1]; // " world "
  pp+=values[0];  // 15
  pp+=values[1];  // 50

  console.log(pp+"Bazinga!");
}

tag`Hello ${ a + b } world ${ a * b}`;

但它给出了

Uncaught SyntaxError: Unexpected token ...(…)

function tag(strings, ...values) {

正如语法错误 Unexpected token ... 告诉您的那样,问题不在于标记,而在于剩余运算符的用法。请尝试以下操作:

var a = 5,
    b = 10;
function tag(strings) {
  var pp="";
  pp+=strings[0]; // "Hello "
  pp+=strings[1]; // " world "
  pp+=arguments[1];  // 15
  pp+=arguments[2];  // 50

  return pp+"Bazinga!";
}

console.log(tag`Hello ${ a + b } world ${ a * b}`);

根据当前Chrome中的ES6 compatibility table, you need to enable rest syntax via the harmony flag