Polyfill for javascript template strings

后端 未结 1 1758
我在风中等你
我在风中等你 2021-01-19 05:09

Are there any polyfills for javascript template strings that use a backtick? I\'m referring to code that looks like this:

var textTemplate = `
   

M

1条回答
  •  离开以前
    2021-01-19 05:34

    Are there any polyfills for javascript template strings that use a backtick?

    No. You can't have a polyfill for new Javascript syntax which the backticks are. A polyfill can't affect the compiling of the code or the execution of a line of code. It can only affect functions calls at run time.

    The solution for new Javascript syntax is to transpile from the new syntax to ES5 compatible code.

    You can't even create a function that would do the same type of string substitution (when passed a normal Javascript quoted string) because the function implementation would not have access to the locally scoped variables that are typically used in backtick templates.

    There are other types of string substitution that can be implemented in a function, but you have to pass the values that you want substituted in (like sprintf in C and use a different syntax than the template literals) rather than just use variable names in the template string.

    0 讨论(0)
提交回复
热议问题