反引号的特点(转载)

 转载自:https://www.css88.com/archives/10088

(本文对读者有帮助的话请移步支持原作者)

JavaScript有3种类型引号:

  单引号('

  双引号("

  反引号 (`)

  前 2 个基本相同:

 

JavaScript 代码:

const test = 'test'
const bike = "bike"

 

使用这 2 种方法几乎没有差别。唯一的区别在于必须转义用于分隔字符串的引号字符:

JavaScript 代码:
const test = 'test'
const test = 'te\'st'
const test = 'te"st'
const test = "te\"st"
const test = "te'st"

 

有各种风格指南,建议始终使用一种风格与另一种风格。

原文作者更喜欢单引号,并且只在 HTML 中使用双引号。向他学习!

反引号 (“) 是 JavaScript 的最新成员,因为它们在 2015 年 ES6 才推出。

它们具有独特的功能:它们允许多行字符串。

使用转义字符,常规字符串也可以转换为多行字符串:

JavaScript 代码:

 
const multilineString = 'A string\non multiple lines'

 

 

使用反引号,者可以避免使用转义字符:

JavaScript 代码:

const multilineString = `A string
on multiple lines`

 

不仅如此。您可以使用 ${} 语法插入变量或表达式:

JavaScript 代码:

const multilineString = `A string
on ${1+1} lines`

 转载自:https://www.css88.com/archives/10088

(本文对读者有帮助的话请移步支持原作者)

posted @ 2018-12-06 20:51  林丶  阅读(641)  评论(0编辑  收藏  举报