String.substitute

Logan Smyth loganfsmyth at gmail.com
Wed Aug 12 15:27:55 UTC 2015


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


At that point, you are implementing a templating language that happens to
match up syntactically with template literals. You'd also then be moving
parsing of the template to runtime instead of compile time, which will slow
things down. It would also only be possible to implement using `eval`.

You can certainly do that, but it doesn't seem like something that should
be included in the language. It's no different from using Handlebars or
Mustache at that point. The primary benefit of template literals is that
they have access to variables in-scope, which seems to be what you are
trying to avoid.

On Wed, Aug 12, 2015 at 8:19 AM, Edwin Reynoso <eorroe at gmail.com> wrote:

> @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
>>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150812/e5c1c718/attachment-0001.html>


More information about the es-discuss mailing list