Attempting to use tagged template tags yields "Uncaught SyntaxError: Unexpected Token"

I use tagged pattern strings in the following code

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}`; 

but he gives

Uncaught SyntaxError: Unexpected token ... (...)

On function tag(strings, ...values) {

+5
source share
1 answer

As the Unexpected token ... syntax error says, not a tag is a problem, but using the rest statement. Try the following:

 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}`); 

According to the ES6 compatibility table , you need to enable the rest syntax using the harmony flag in current Chrome.

+4
source

Source: https://habr.com/ru/post/1233699/


All Articles