Discuss / JavaScript / 为什么我这里输出的是Hello, ${name}!

为什么我这里输出的是Hello, ${name}!

Topic source

ApacheTomCat

#1 Created at ... [Delete] [Delete and Lock User]

老师,为啥我这里输出的是Hello, ${name}!。 也没看到老师的代码有引用jQuery啊。

ApacheTomCat

#2 Created at ... [Delete] [Delete and Lock User]

找到问题了, 我写的是let s = 'Hello, ${name}!'; 正确的是let s = Hello, ${name}!;

ApacheTomCat

#3 Created at ... [Delete] [Delete and Lock User]

在 ES6 中引入了一种新的字符串字面量 — 模板字符串,除了使用反引号 () 表示,它们看上去和普通的字符串没有什么区别。在最简单的情况下,他们就是普通的字符串: context.fillText(Ceci n'est pas une cha?ne., x, y); context.fillText(Ceci n'est pas une cha?ne.`, x, y);

之所以被称为模板字符串,是因为模板字符串为 JS 引入了简单的字符串插值特性,也就是说,可以方便优雅地将 JS 的值插入到字符串中。


  • 1

Reply