Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

javascript - JavaScript中反引号字符(`)的用法?(Usage of the backtick character (`) in JavaScript?)

In JavaScript, a backtick ? seems to work the same as a single quote.(在JavaScript中,反引号?的作用似乎与单引号相同。)

For instance, I can use a backtick to define a string like this:(例如,我可以使用反引号定义这样的字符串:) var s = `abc`; Is there any way in which the behavior of the backtick actually differs from that of a single quote?(反引号的行为实际上有什么不同于单引号的行为?) ? Note that among programmers, "backtick" is one name for what is more generally called the grave accent .(?请注意,在程序员中,“反勾号”是通常被称为重音符号的一个名称。) Programmers also sometimes use the alternate names "backquote" and "backgrave".(程序员有时还会使用备用名称 “ backquote”和“ backgrave”。) Also, on Stack Overflow and elsewhere, other common spellings for "backtick" are "back-tick" and "back tick".(同样, 在Stack Overflow和其他地方,“ backtick”的其他常见拼写是“ back-tick”和“ back tick”。)   ask by caesarwang translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a feature called template literals .(这是称为模板文字的功能。)

They were called "template strings" in prior editions of the ECMAScript 2015 specification.(在ECMAScript 2015规范的先前版本中,它们被称为“模板字符串”。) Template literals are supported by Firefox 34, Chrome 41, and Edge 12 and above, but not by Internet Explorer.(Firefox 34,Chrome 41和Edge 12及更高版本支持模板文字,但Internet Explorer不支持。) Examples: http://tc39wiki.calculist.org/es6/template-strings/(示例: http//tc39wiki.calculist.org/es6/template-strings/) Official specification: ECMAScript 2015 Language Specification, 12.2.9 Template Literal Lexical Components (a bit dry)(官方规范: ECMAScript 2015语言规范,12.2.9模板文字词汇组件 (有点干)) Template literals can be used to represent multi-line strings and may use "interpolation" to insert variables:(模板文字可以用于表示多行字符串,并且可以使用“插值”来插入变量:) var a = 123, str = `--- a is: ${a} ---`; console.log(str); Output:(输出:) --- a is: 123 --- What is more important, they can contain not just a variable name, but any Javascript expression:(更重要的是,它们不仅可以包含变量名,还可以包含任何Javascript表达式:) var a = 3, b = 3.1415; console.log(`PI is nearly ${Math.max(a, b)}`);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...