I am starting to use template literals to make a error generator.
I have working code, but I am forced to declare the list of possible errors inside the constr
It's a darn shame that template literals aren't more flexible.
Here are two methods that may be close to what you want.
var s = (item, price) => {return `item: ${item}, price: $${price}`}
s('pants', 10) // 'item: pants, price: $10'
s('shirts', 15) // 'item: shirts, price: $15'
To generalify:
var s = () => {return ``}
If you are not running E6, you could also do:
var s = function(){return ``}
This seems to be a bit more concise than the previous answers.
https://repl.it/@abalter/reusable-JS-template-literal
class Person
{
constructor (first, last)
{
this.first = first;
this.last = last;
}
sayName ()
{
return `Hi my name is ${this.first} ${this.last}`;
}
}
var bob = new Person("Bob", "Jones")
console.log(bob.sayName()) // Hi my name is Bob Jones
console.log(new Person("Mary", "Smith").sayName()) // Hi my name is Mary Smith
https://repl.it/@abalter/late-evaluation-of-js-template-literal