javascript - Can you extract var name from interpolated expression with template literals? -
if have var like
myvar = `some text ${eggs} , ${noodles} or ${pies}`; is there way myvar unprocessed string before var substitution, "some text ${eggs} , ${noodles} or ${pies}"?
specifically, i'd able dynamically generate array var names (eggs, noodles, pies) inside. i've got many different myvars , want avoid having explicitly type arrays out.
from template literals, can not take variables out of literal string.
instead of using fixed set of variables, suggets use dynamic approach array, or other structured data organisation.
var gettext = (...a) => `some text ${a[0]} , ${a[1]} or ${a[2]}`; console.log(gettext('foo', 'bar', 'baz')); console.log(gettext('eggs', 'noodles', 'pies'));
Comments
Post a Comment