String.prototype.replaceAll
Promise.any
逻辑运算符和赋值表达式
数值分隔符
Intl.ListFormat
Intl.DateTimeFormat 的 dateStyle 和 timeStyle 选项
在 JavaScript 中,replace() 方法仅替换字符串中一个模式的第一个实例。如果我们要替换字符串中某个模式的所有匹配项,则唯一的方法是使用全局正则表达式。
let str = 'I use linux, I love linux'str = str.replaceAll('linux', 'windows');console.log(str)/**** Output ****/// I use windows, I love windows
ES2021 将引入 Promise.any() 方法,只要这个方法命中了 Promise 列表 / 数组中的第一个已解析的 Promise,就会短路并返回一个值(如示例 1a 中所述)。如果所有的 promise 都被拒绝,那么它将抛出一个汇总错误消息(如示例 1b 所示)。
它与 Promise.race() 不同,因为一旦给定的 Promise 之一被解析或拒绝,Promise.any() 方法就会短路。
Promise.any([new Promise((resolve, reject) => setTimeout(reject, 200, 'Third')),new Promise((resolve, reject) => setTimeout(resolve, 1000, 'Second')),new Promise((resolve, reject) => setTimeout(resolve, 2000, 'First')),]).then(value => console.log(`Result: ${value}`)).catch (err => console.log(err))/**** Output ****/// Result: Second
Promise.any([Promise.reject('Error 1'),Promise.reject('Error 2'),Promise.reject('Error 3')]).then(value => console.log(`Result: ${value}`)).catch (err => console.log(err))/**** Output ****/// AggregateError: All promises were rejected
// Assignment Operator Examplelet num = 5num+=10console.log(num) // 15// Logical Operator Examplelet num1 = 6let num2 = 3console.log(num1 === 6 && num2 === 2) // falseconsole.log(num1 === 6 || num2 === 2) // true
新的提案让我们将能把逻辑运算符和赋值运算符结合起来。以下是 &&、||和?? 运算符的一些示例:
// Logical Assignment Operator with && operatorlet num1 = 5let num2 = 10num1 &&= num2console.log(num1) // 10// Line 5 can also be written as following ways// 1. num1 && (num1 = num2)// 2. if (num1) num1 = num2
// Logical Assignment Operator with || operatorlet num1let num2 = 10num1 ||= num2console.log(num1) // 10// Line 5 can also be written as following ways// 1. num1 || (num1 = num2)// 2. if (!num1) num1 = num2
// Logical Assignment Operator with ?? operatorlet num1let num2 = 10num1 ??= num2console.log(num1) // 10num1 = falsenum1 ??= num2console.log(num1) // false// Line 5 can also be written as following ways// num1 ?? (num1 = num2)
let number = 100_000console.log(number)/**** Output ****/// 100000
new Intl.ListFormat([locales[, options]])
Intl.ListFormat 有一个称为 format() 的方法,该方法接收一个数组作为一个参数,并以特定于语言环境的方式对其进行格式化。
const arr = ['Pen', 'Pencil', 'Paper']let obj = new Intl.ListFormat('en', { style: 'short', type: 'conjunction' })console.log(obj.format(arr))/**** Output ****/// Pen, Pencil, & Paperobj = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' })console.log(obj.format(arr))/**** Output ****/// Pen, Pencil, and Paperobj = new Intl.ListFormat('en', { style: 'narrow', type: 'conjunction' })console.log(obj.format(arr))/**** Output ****/// Pen, Pencil, Paper// Passing in Italy language tagobj = new Intl.ListFormat('it', { style: 'short', type: 'conjunction' })console.log(obj.format(arr))/**** Output ****/// Pen, Pencil e Paper// Passing in German language tagobj = new Intl.ListFormat('de', { style: 'long', type: 'conjunction' })console.log(obj.format(arr))/**** Output ****/// Pen, Pencil und Paper
Intl.DateTimeFormat 对象是用来启用语言敏感的日期和时间格式的对象构造器。新提案的 dateStyle 和 timeStyle 选项可用于请求给定长度的,特定于语言环境的日期和时间。
// Time only with short formatlet o = new Intl.DateTimeFormat('en' , { timeStyle: 'short' })console.log(o.format(Date.now()))// 11:27 PM// Time only with medium formato = new Intl.DateTimeFormat('en' , { timeStyle: 'medium'})console.log(o.format(Date.now()))// 11:27:57 PM// Time only with long formato = new Intl.DateTimeFormat('en' , { timeStyle: 'long' })console.log(o.format(Date.now()))// 11:27:57 PM GMT+11// Date only with short formato = new Intl.DateTimeFormat('en' , { dateStyle: 'short'})console.log(o.format(Date.now()))// 10/6/20// Date only with medium formato = new Intl.DateTimeFormat('en' , { dateStyle: 'medium'})console.log(o.format(Date.now()))// Oct 6, 2020// Date only with long formato = new Intl.DateTimeFormat('en' , { dateStyle: 'long'})console.log(o.format(Date.now()))// October 6, 2020
let abc// English languageabc = new Intl.DateTimeFormat('en' , { timeStyle: 'short', dateStyle: 'long'})console.log(abc.format(Date.now()))// October 6, 2020 at 11:40 PM// Italian languageabc = new Intl.DateTimeFormat('it' , { timeStyle: 'short', dateStyle: 'long'})console.log(abc.format(Date.now()))// 6 ottobre 2020 23:40// German languageabc = new Intl.DateTimeFormat('de' , { timeStyle: 'short', dateStyle: 'long'})console.log(abc.format(Date.now()))// 6. Oktober 2020 um 23:40
作为开发人员,跟紧语言的新特性是很重要的。如果你错过了 ES2020 的特性更新,建议你阅读这篇文章:
https://codeburst.io/exciting-features-of-javascript-es2021-es12-1de8adf6550b
InfoQ 写作平台欢迎所有热爱技术、热爱创作、热爱分享的内容创作者入驻!
还有更多超值活动等你来!
扫描下方二维码
填写申请,成为作者
点个在看少个 bug 👇