| Invalid Date
字数 0阅读时长 1 分钟
notion image
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
They were called "template strings" in prior editions of the ES2015 specification.

Syntax

Description

Template literals are enclosed by the backtick (` `) (grave accent) character instead of double or single quotes.
Template literals can contain placeholders. These are indicated by the dollar sign and curly braces (${expression}). The expressions in the placeholders and the text between the backticks (` `) get passed to a function.
The default function just concatenates the parts into a single string. If there is an expression preceding the template literal (tag here), this is called a tagged template. In that case, the tag expression (usually a function) gets called with the template literal, which you can then manipulate before outputting.
To escape a backtick in a template literal, put a backslash (\) before the backtick.

Multi-line strings

Any newline characters inserted in the source are part of the template literal.
Using normal strings, you would have to use the following syntax in order to get multi-line strings:
Using template literals, you can do the same like this:

Expression interpolation

In order to embed expressions within normal strings, you would use the following syntax:
Now, with template literals, you are able to make use of the syntactic sugar, making substitutions like this more readable:

Nesting templates

In certain cases, nesting a template is the easiest (and perhaps more readable) way to have configurable strings. Within a backticked template, it is simple to allow inner backticks simply by using them inside a placeholder ${ } within the template.
For instance, if condition a is true, then return this templated literal.
In ES5:
In ES2015 with template literals and without nesting:
In ES2015 with nested template literals:

Tagged templates

A more advanced form of template literals are tagged templates.
Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string values. The remaining arguments are related to the expressions.
The tag function can then perform whatever operations on these arguments you wish, and return the manipulated string. (Alternatively, it can return something completely different, as described in one of the following examples.)
The name of the function used for the tag can be whatever you want.
Tag functions don't even need to return a string!

Raw strings

The special raw property, available on the first argument to the tag function, allows you to access the raw strings as they were entered, without processing escape sequences.
In addition, the String.raw() method exists to create raw strings—just like the default template function and string concatenation would create.

Tagged templates and escape sequences

As of ECMAScript 2016, tagged templates conform to the rules of the following escape sequences:
  • Unicode escapes started by "\u", for example \u00A9
  • Unicode code point escapes indicated by "\u{}", for example \u{2F804}
  • Hexadecimal escapes started by "\x", for example \xA9
  • Octal literal escapes started by "\0o" and followed by one or more digits, for example \0o251
This means that a tagged template like the following is problematic, because, per ECMAScript grammar, a parser looks for valid Unicode escape sequences, but finds malformed syntax:

ES2018 revision of illegal escape sequences

Tagged templates should allow the embedding of languages (for example DSLs, or LaTeX), where other escapes sequences are common. The ECMAScript proposal Template Literal Revision (Stage 4, to be integrated in the ECMAScript 2018 standard) removes the syntax restriction of ECMAScript escape sequences from tagged templates.
However, illegal escape sequences must still be represented in the “cooked” representation. They will show up as undefined element in the “cooked” array:
Note that the escape sequence restriction is only dropped from tagged templates—not from untagged template literals:

Specifications

Browser compatibility

See also

Loading...
目录