String.substitute

Tab Atkins Jr. jackalmage at gmail.com
Wed Aug 12 15:08:32 UTC 2015


On Wed, Aug 12, 2015 at 7:31 AM, Edwin Reynoso <eorroe at gmail.com> wrote:
> Yes of course, still requires 1.Destructuring, and making a function for
> each type of string to return. Defeats the purpose.
>
> I'd have to make different functions for each template:
>
> ```JS
> const yearTemplate = ({ year }) => `This year is ${year}`;
>
> const ageTemplate = ({ age}) => `I'm ${age} yrs old`;
> ```
>
> Compare to:
>
> ```JS
> let yearSentence = String.substitute({ year: 2015}, `This year is ${year}`);
> let ageSentence = String.substitute({ age:100 }, `I'm ${age} yrs old`);
> ```

let yearSentence = ({year:2015}=>`This year is ${year}`)();

should work, or

let yearSentence = (year=>`This year is ${year}`)(2015);

You don't *have* to save the templates in variables. You can just call
them immediately.

~TJ


More information about the es-discuss mailing list