The supplant function is easy to use for string interpolation and a nice introduction templating in JavaScript.
Here’s the function from Douglas Crockford’s website:
|
|
So how is it used. Here’s a quick example:
var greeting = "Hi, I'm {name}".supplant[{name:"Richard"}];
Output: “Hi, I’m Richard”
Another example:
|
|
Output: “Hi, my name is Richard and I’m from Columbus, Ohio”
You can use it with arrays:
|
|
Output: “My classmates are Richard, Yalcin, Dan, Mike, and Kerry”
These are a few examples, hopefully you do find them helpful. Supplant does take some criticism for being inefficient as well as unescapable, which are both founded. So, you may want to use supplant sparingly, but it’s certainly a great tool in learning JavaScript.
Here’s a stack overflow post that discusses it further as well.
And as you delve deeper into string interpolation and templating, I suggest looking into Handlebars.js or Mustache.
As always, if you have any questions, please don’t hesitate to ask.
Cheers!