ecmascript 6 - ES6 template strings as variable? -
seems template strings useful thing pass module, let's want let calling code provide how want format output.
thing is, @ least in node repl, appears template string evaluated immediately, can't. example:
var template = `time: ${now} | message: ${thing.msg}`; var thing = {msg : 'something wicked way comes'}; var = new date(); attempting enter 3 lines repl error out thing has not yet been defined on line of template.
is there way around this? i'd pass template string around variable.
note saw question "dumbing down" template strings before asking this. it's not same question @ all, i'm asking deferring execution, not converting normal string.
the thing can think of wrapping template in lambda defer execution. not sure if that's useful use case? i'm thinking of like:
var template = (now, thing) => `time: ${now} | message: ${thing && thing.msg}`; var thing = {msg : 'something wicked way comes'}; var = new date(); console.log(template(now, thing)); using ${thing && thing.msg} instead of ${thing.msg} prevents console error, return 'message: undefined' if object doesn't exist.
Comments
Post a Comment