es6和es2018语法_ES2018:Javascript在2018年的新功能

es6和es2018语法

by Flavio H. Freitas

Flavio H.Freitas着

ES2018:Javascript在2018年的新功能 (ES2018: Javascript's new features in 2018)

Our friends from TC39 have released new updates for our beloved JavaScript language.

来自TC39的朋友已经发布了我们钟爱JavaScript语言的新更新。

If you want to follow the process of the new releases by the committee, you can access this link. The process of approving and making a change goes through five stages:

如果您想按照委员会的要求发布新版本,可以访问此链接 。 批准和进行更改的过程分为五个阶段:

  • Stage 0: Strawman — Allow input into the specification

    阶段0:稻草人-允许输入规范
  • Stage 1: Proposal — Make the case for the addition; Describe the shape of a solution; Identify potential challenges

    第1阶段:提案-为增加理由。 描述解决方案的形状; 找出潜在的挑战
  • Stage 2: Draft — Precisely describe the syntax and semantics using formal spec language

    第2阶段:草稿-使用正式规范语言精确描述语法和语义
  • Stage 3: Candidate — Indicate that further refinement will require feedback from implementations and users

    第3阶段:候选者-表示进一步的改进将需要实现和用户的反馈
  • Stage 4: Finished — Indicate that the addition is ready for inclusion in the formal ECMAScript standard

    第4阶段:已完成-表示已准备好将其添加到正式的ECMAScript标准中

More details can be seen here. If you want to learn more about the previous changes, check out ES6, ES7 and ES8.

更多细节可以在这里看到。 如果您想了解有关先前更改的更多信息,请查看ES6ES7ES8

So let’s see what they have added or updated in the past year:

因此,让我们看看他们在过去一年中添加或更新的内容:

1.很多正则表达式更改 (1. Lots of Regex changes)

We have four modifications for regex. Let’s see them:

正则表达式有四个修改。 让我们看看他们:

s ( dotAll )标志用于正则表达式 (s (dotAll) flag for regular expressions)

While using regular expressions, you expect that the dot . matches a single character, but it’s not always true. One exception is with line terminator characters:

使用正则表达式时,您希望使用点号. 匹配单个字符,但并非总是如此。 行终止符是一种例外:

/hello.bye/.test('hello\nbye') // prints false

The solution is the new flag /s (from singleline):

解决方案是新标志/ s(从单行开始):

/hello.bye/s.test('hello\nbye')  // prints true
RegExp命名捕获组 (RegExp named capture groups)

This is the old way of getting the year, month, and day from a date:

这是从日期获取年,月和日的旧方法:

const REGEX = /([0-9]{4})-([0-9]{2})-([0-9]{2});const results = REGEX.exec('2018-07-12');console.log(results[1]); // prints 2018console.log(results[2]); // prints 07console.log(results[3]); // prints 12

And if you are working with a long regex, you know how hard it is to keep track of the groups, parentheses, and the indices. With the new named capture group, it's possible to:

而且,如果您使用的是长的正则表达式,那么您就会知道跟踪组,括号和索引有多么困难。 使用新命名的捕获组,可以:

const REGEX = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2});const results = REGEX.exec('2018-07-12');console.log(results.groups.year);  // prints 2018console.log(results.groups.month); // prints 07console.log(results.groups.day);   // prints 12
RegExp回顾断言 (RegExp Look behind Assertions)

There are two versions of look behind assertions: positive and negative.

断言背后有两种外观:肯定和否定。

a) Positive (?<=…)

a)正面(?< =…)

'$foo #foo @foo'.replace(/(?<=#)foo/g, 'XXX')// prints $foo #XXX @foo

This (?<=#)foo/g regex says that the word must start with # and it doesn’t contribute to the overall matched string (so it won't replace the # character).

这个(?<=#)fo o / g正则表达式表示该单词必须以#开头,并且不会对整体匹配的字符串产生影响(因此它不会替换#字符)。

b) Negative (?<!…)

b)负数(?< !...)

'$foo #foo @foo'.replace(/(?<!#)foo/g, 'XXX')// prints $XXX #foo @XXX

On the contrary, this assertion guarantees that it doesn't start with #.

相反,此断言保证其不以#开头。

RegExp Unicode属性转义 (RegExp Unicode Property Escapes)

Now we can search for characters by mentioning their Unicode character property inside of \p{}

现在,我们可以通过在\p{}提及Unicode字符属性来搜索字符

/\p{Script=Greek}/u.test('μ') // prints true

You can check out more of the properties by clicking here.

您可以通过单击此处检查更多属性。

2.休息/传播属性 (2. Rest/Spread Properties)

The rest operator (...) copies the remaining property keys that were not mentioned. Let's look at an example:

其余运算符(...)复制未提及的其余属性键。 让我们看一个例子:

const values = {a: 1, b: 2, c: 3, d: 4};const {a, ...n} = values;console.log(a);   // prints 1console.log(n);   // prints {b: 2, c: 3, d: 4}

3. Promise.prototype finally (3. Promise.prototype finally)

This new callback will always be executed, if catch was called or not.

无论是否调用catch,都会始终执行此新的回调。

fetch('http://website.com/files').then(data => data.json()).catch(err => console.error(err)).finally(() => console.log('processed!'))

4.异步迭代 (4. Asynchronous Iteration)

Finally!

最后!

Now we can use await on our loops declarations.

现在,我们可以在循环声明中使用await了。

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

And these are all the changes from this year. Let’s wait to see what they will bring us next year.

这些都是今年以来的所有变化。 让我们拭目以待,看看他们明年会带给我们什么。

If you enjoyed this article, be sure to like it give me a lot of claps — it means the world to the writer ? And follow me if you want to read more articles about Culture, Technology, and Startups.

如果您喜欢这篇文章,请确保喜欢它给了我很多鼓掌-这对作家来说意味着世界? 如果您想阅读更多有关文化,技术和创业的文章,请跟随我

Flávio H. de Freitas is an Entrepreneur, Engineer, Tech lover, Dreamer and Traveler. Has worked as CTO in Brazil, Silicon Valley and Europe.

FlávioH. de Freitas是一位企业家,工程师,技术爱好者,梦想家和旅行者。 曾在巴西硅谷和欧洲担任首席技术官

翻译自: https://www.freecodecamp.org/news/es9-javascripts-state-of-art-in-2018-9a350643f29c/

es6和es2018语法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值