String.substitute

Edwin Reynoso eorroe at gmail.com
Wed Aug 12 15:19:19 UTC 2015


@logan that's an interesting thought, which is why I posted this for
discussion, Thinking of that, I'm kind of doubting most will like the
function to only be able to take the literal instead as a variable because
that's just messing with the way Javascript itself.

Could be possible to pass a string with "" instead of \`\`

```JS
String.substitute({year: 2015}, "This year is ${year}");
```

Which is giving javascript a way to allow writing a Template Literal
without evaluating by using "" instead of \`\`

@Tab atkin

Your still making a function yourself. You may ask what's wrong with that?

Well then I'd say what's the point of `ES6` having `"Hi".includes("H")`
when we could of just did:

```JS
function includes(str, included) {
  return str.indexOf(included) > -1;
}
```

On Wed, Aug 12, 2015 at 11:08 AM, Tab Atkins Jr. <jackalmage at gmail.com>
wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150812/7ca1c012/attachment.html>


More information about the es-discuss mailing list