javascript - javascript中 ` and '之间的主要区别是什么

标签 javascript angular typescript

我是 typescript /javascript/Angular 新手,我正在阅读一些教程,我不断遇到以下类型的事情:

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  toString() {
    return `(${this.x}, ${this.y})`;
  }
}

现在我的问题是这些是物理反引号,我所看到的其他所有内容都只是使用“和”,我相信它们在功能上是相同的,所以这两个是真的:

  1. ` == '

  2. ` === '

或者这只是 Angular/Typescript 的事情吗?

最佳答案

Template literals are enclosed by the back-tick (`) character instead of double " or single ' quotes. They can contain placeholders, indicated by the dollar sign and curly braces (${expression}). The expressions in the placeholders and the text between them get passed to a function. The default function just concatenates the parts into a single string.

- Source: MDN web docs

您可以使用黑色勾号 ` 在字符串中插入 JavaScript 符号。例如:

const name = 'world'

// using ''
let myString1 = 'Hello' + name;

// using ``
let myString2 = `Hello ${name}`

myString1myString2 都具有相同的字符串。

这是在 JavaScript 中格式化内容的更方便的方式,无需连接字符串,您可以在字符串中插入变量。

是的,` 等于 '。尝试在控制台中输入以下内容:

`\`` === '`'

它将返回为true

关于javascript - javascript中 ` and '之间的主要区别是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625527/

相关文章:

javascript - 如何中断和停止 JavaScript 中正在运行的函数?

javascript - 从 Angular 4 升级到 7 导致问题

javascript - 在带有以太 JS 的 Next Js 中使用 window.ethereum 时出错

javascript - 克隆 JavaScript 事件对象

javascript - React - 从另一个组件中的路由路径获取参数

angular - 如何在 Angular 5 中动态使用这个管道?

angular - 在另一个 API 调用中显示来自 API 调用的数据

typescript - 使用属性装饰器对属性类型进行严格类型检查

angular - 在 angular 2/angular 4 中集成 recaptcha

javascript - Ajax 验证用户名和密码