ES2018(ES9) 的重大新特性

2018-11-0710:46:11WEB前端开发Comments4,362 views字数 2969阅读模式
ES2018 是 ECMAScript 标准的最新版本。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

它引入了哪些新东西呢?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

Rest(剩余)/Spread(展开) 属性

ES6 在处理数组解构时,引入了 rest(剩余)元素的概念,例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

const numbers = [1, 2, 3, 4, 5]
[first, second, ...others] = numbers

还有展开元素时:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

const numbers = [1, 2, 3, 4, 5]
const sum = (a, b, c, d, e) => a + b + c + d + e
const sum = sum(...numbers)

ES2018 为对象引入了类似的功能。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

rest(剩余) 属性文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

const { first, second, ...others } = { first: 1, second: 2, third: 3, fourth: 4, fifth: 5 }
first // 1
second // 2
others // { third: 3, fourth: 4, fifth: 5 }

spread(展开) 属性 允许通过组合展开运算符 ... 之后传递的对象属性来创建新对象:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

const items = { first, second, ...others }
items //{ first: 1, second: 2, third: 3, fourth: 4, fifth: 5 }

Asynchronous iteration (异步迭代)

新的 for-await-of 构造允许您使用异步可迭代对象作为循环迭代:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

for await (const line of readLines(filePath)) {
console.log(line)
}

由于这使用 await ,你只能在异步函数中使用它,就像普通的 await 一样(参见 async / await 章节)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

Promise.prototype.finally()

当一个 promise 得到满足(fulfilled)时,它会一个接一个地调用 then() 方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

如果在此期间发生错误,则跳过 then() 方法并执行 catch()方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

finally() 允许您运行一些代码,无论 promise 的执行成功或失败:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

fetch('file.json')
.then(data => data.json())
.catch(error => console.error(error))
.finally(() => console.log('finished'))

正则表达式改进

先行断言(lookahead) 和 后行断言(lookbehind)

正则表达式后行断言(lookbehind):根据前面的内容匹配字符串。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

下面是一个先行断言(lookahead):您可以使用 ?= 匹配一个字符串,该字符串后面跟着一个特定的子字符串:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

/Roger(?=Waters)/
/Roger(?= Waters)/.test('Roger is my dog') //false
/Roger(?= Waters)/.test('Roger is my dog and Roger Waters is a famous musician') //true

?! 执行逆操作,匹配一个字符串,该字符串后面没有一个特定的子字符串:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

/Roger(?!Waters)/
/Roger(?! Waters)/.test('Roger is my dog') //true
/Roger(?! Waters)/.test('Roger Waters is a famous musician') //false

先行断言(lookahead)使用 ?= 符号。它们已经可用了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

后行断言(lookbehind),是一个新功能,使用 ?< =文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

/(?<=Roger) Waters/
/(?<=Roger) Waters/.test('Pink Waters is my dog') //false
/(?<=Roger) Waters/.test('Roger is my dog and Roger Waters is a famous musician') //true

后行断言(lookbehind) 逆操作,使用 ?< !文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

/(?<!Roger) Waters/
/(?<!Roger) Waters/.test('Pink Waters is my dog') //true
/(?<!Roger) Waters/.test('Roger is my dog and Roger Waters is a famous musician') //false

Unicode 属性转义 \p{…} 和 \P{…}

在正则表达式模式中,您可以使用 \d 匹配任何数字,\s 匹配任何不为空格的字符,\w 匹配任何字母数字字符,依此类推。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

这个新功能将扩展此概念到引入 \p{} 匹配所有 Unicode 字符,否定为 \P{} 。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

任何 unicode 字符都有一组属性。 例如,Script 确定语言系列,ASCII 是一个布尔值, 对于 ASCII 字符,值为 true,依此类推。 您可以将此属性放在花括号中,正则表达式将检查是否为真:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

/^\p{ASCII}+$/u.test('abc')   //✅
/^\p{ASCII}+$/u.test('ABC@')  //✅
/^\p{ASCII}+$/u.test('ABC?') //❌

ASCII_Hex_Digit 是另一个布尔属性,用于检查字符串是否仅包含有效的十六进制数字:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

/^\p{ASCII_Hex_Digit}+$/u.test('0123456789ABCDEF') //✅
/^\p{ASCII_Hex_Digit}+$/u.test('h')                //❌

还有许多其他布尔属性,您只需通过在花括号中添加它们的名称来检查它们,包括 UppercaseLowercaseWhite_SpaceAlphabeticEmoji 等:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

/^\p{Lowercase}$/u.test('h') //✅
/^\p{Uppercase}$/u.test('H') //✅
/^\p{Emoji}+$/u.test('H')   //❌
/^\p{Emoji}+$/u.test('??') //✅

除了这些二进制属性之外,您还可以检查任何 unicode 字符属性以匹配特定值。在这个例子中,我检查字符串是用希腊语还是拉丁字母写的:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

/^\p{Script=Greek}+$/u.test('ελληνικά') //✅
/^\p{Script=Latin}+$/u.test('hey') //✅

您可以直接在提案上阅读可使用的 所有属性 。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

命名捕获组(Named capturing groups)

在 ES2018 中,可以为捕获组分配一个名称,而不是仅在结果数组中分配一个 slot(插槽):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

const re = /(?\d{4})-(?\d{2})-(?\d{2})/
const result = re.exec('2015-01-02')
// result.groups.year === '2015';
// result.groups.month === '01';
// result.groups.day === '02';

正则表达式的 ‘s’ 标志

s 标志是 ‘single line'(单行)的缩写,它使 . 匹配新的行字符。如果没有它,点将匹配普通字符,而不是新行:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html

/hi.welcome/.test('hi\nwelcome') // false
/hi.welcome/s.test('hi\nwelcome') // true
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7747.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/7747.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定