我是 typescript /javascript/Angular 新手,我正在阅读一些教程,我不断遇到以下类型的事情:
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return `(${this.x}, ${this.y})`;
}
}
现在我的问题是这些是物理反引号,我所看到的其他所有内容都只是使用“和”,我相信它们在功能上是相同的,所以这两个是真的:
` == '
?` === '
?
或者这只是 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}`
myString1
和 myString2
都具有相同的字符串。
这是在 JavaScript 中格式化内容的更方便的方式,无需连接字符串,您可以在字符串中插入变量。
是的,`
等于 '
。尝试在控制台中输入以下内容:
`\`` === '`'
它将返回为true
关于javascript - javascript中 ` and '之间的主要区别是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625527/