ES6 Template Literals: How to pass a scope before they are interpreted?

后端 未结 3 375
清酒与你
清酒与你 2021-01-01 04:36

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

3条回答
  •  悲&欢浪女
    2021-01-01 05:14

    It's a darn shame that template literals aren't more flexible.

    Here are two methods that may be close to what you want.

    First:

    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 `