var text = `Now is the time for all good men to come to the aid of their country!`; console.log( text ); // Now is the time for all good men // to come to the aid of their // country!
var name = "Kyle"; var greeting = "Hello " + name + "!"; console.log( greeting ); // "Hello Kyle!" console.log( typeof greeting ); // "string"
var name = "Kyle"; var greeting = `Hello ${name}!`; console.log( greeting ); // "Hello Kyle!" console.log( typeof greeting ); // "string"
function foo(str) { var name = "foo"; console.log( str ); //name str str typeof 'string' } function bar() { var name = "bar"; foo( `Hello from ${name}!` ); } var name = "global"; bar();
function upper(s) { return s.toUpperCase(); } var who = "reader"; var text = `A very ${upper( "warm" )} welcome to all of you ${upper( `${who}s` )}!`; console.log( text ); // A very WARM welcome // to all of you READERS!
function foo(strings, ...values) { console.log( strings ); console.log( values ); } var desc = "awesome"; foo`Everything is ${desc}!`; // [ "Everything is ", "!"] // [ "awesome" ]
tag foo
, a kind of function call without (...)
. Thus, we can process the string additionally.foo
function gets an array of strings that were around the ${desk}
expression, and the actual values of the expressions themselves. const greeting = lang`Hello ${ name }!`
const greeting = `${ lang('Hello') } ${ name }!`
const greeting = lang('Hello') + name + '!';
function lang(strings, ...values) { return strings.reduce( function(s,v,idx){ return s + (idx > 0 ? values[idx-1] : "") + v; }, "" ); }
const lang = l = (strings, ...values) => strings.reduce((prevString, nextString, index) => prevString + (index > 0 ? values[index - 1] : '') + translate(nextString), ''); // translate :)
const name = ''; let greeting = lang`Hello ${ name }, how are you?`; // greeting = l`Hello ${ name }, how are you?`; // , ?
function dollabillsyall(strings, ...values) { return strings.reduce( function(s,v,idx){ if (idx > 0) { if (typeof values[idx-1] == "number") { // look, also using interpolated // string literals! s += `$${values[idx-1].toFixed( 2 )}`; } else { s += values[idx-1]; } } return s + v; }, "" ); } var amt1 = 11.99, amt2 = amt1 * 1.08, name = "Kyle"; var text = dollabillsyall `Thanks for your purchase, ${name}! Your product cost was ${amt1}, which with tax comes out to ${amt2}.` console.log( text ); // Thanks for your purchase, Kyle! Your // product cost was $11.99, which with tax // comes out to $12.95.
In addition, a context is often needed (so that “name” in some cases is translated as “name”, and in others - “name”), where you can add external information (gender {M / F}, number { singular / plural}, and so on and so on).
function bar() { return function foo(strings, ...values) { console.log( strings ); console.log( values ); } } const desc = 'OK'; bar()`Everything is ${desc}!`;
lang('ru')`Everything is ${desc}!`; // lang('name', 'number')`${name}'s team is about ${count}`;
Source: https://habr.com/ru/post/279595/
All Articles