=========== // Logical Assignment Operator with && operator let num1 = 5 let num2 = 10 num1 &&= num2 console.log(num1) // 10 // Line 5 can also be written as following ways // 1. num1 && (num1 = num2) // 2. if (num1) num1 = num2 带有||的运算符逻辑赋值运算符 仅当 LHS 值为假时,才将 RHS 变量值赋给 LHS 变量。
=========== // Logical Assignment Operator with || operator let num1 let num2 = 10 num1 ||= num2 console.log(num1) // 10 // Line 5 can also be written as following ways // 1. num1 || (num1 = num2) // 2. if (!num1) num1 = num2 带有?? 运算符的逻辑赋值运算符 ES2020 引入了空值合并运算符,其也可以与赋值运算符结合使用。仅当 LHS 为 undefined 或仅为 null 时,才将 RHS 变量值赋给 LHS 变量。
=========== // Logical Assignment Operator with ?? operator let num1 let num2 = 10 num1 ??= num2 console.log(num1) // 10 num1 = false num1 ??= num2 console.log(num1) // false // Line 5 can also be written as following ways // num1 ?? (num1 = num2) 数值分隔符 新引入的数值分隔符使用 _(下划线)字符,在数值组之间提供分隔,使数值读起来更容易。例如:
=========== let number = 100_000 console.log(number) /**** Output ****/ // 100000 Intl.ListFormat ListFormat 对象带有两个参数,它们都是可选的。第一个参数是语言(语言环境),第二个参数是具有两个属性(样式和类型)的选项对象。
=========== 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, & Paper
obj = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' }) console.log(obj.format(arr)) /**** Output ****/ // Pen, Pencil, and Paper
obj = new Intl.ListFormat('en', { style: 'narrow', type: 'conjunction' }) console.log(obj.format(arr)) /**** Output ****/ // Pen, Pencil, Paper
// Passing in Italy language tag obj = new Intl.ListFormat('it', { style: 'short', type: 'conjunction' }) console.log(obj.format(arr)) /**** Output ****/ // Pen, Pencil e Paper
// Passing in German language tag obj = new Intl.ListFormat('de', { style: 'long', type: 'conjunction' }) console.log(obj.format(arr)) /**** Output ****/ // Pen, Pencil und Paper dateStyle 和 timeStyle 选项 Intl.DateTimeFormat 对象是用来启用语言敏感的日期和时间格式的对象构造器。新提案的 dateStyle 和 timeStyle 选项可用于请求给定长度的,特定于语言环境的日期和时间。
下面是不同的选项和语言(区域设置)的一些示例:
=========== // Time only with short format let o = new Intl.DateTimeFormat('en' , { timeStyle: 'short' }) console.log(o.format(Date.now())) // 11:27 PM
// Time only with medium format o = new Intl.DateTimeFormat('en' , { timeStyle: 'medium'}) console.log(o.format(Date.now())) // 11:27:57 PM
// Time only with long format o = new Intl.DateTimeFormat('en' , { timeStyle: 'long' }) console.log(o.format(Date.now())) // 11:27:57 PM GMT+11
// Date only with short format o = new Intl.DateTimeFormat('en' , { dateStyle: 'short'}) console.log(o.format(Date.now())) // 10/6/20
// Date only with medium format o = new Intl.DateTimeFormat('en' , { dateStyle: 'medium'}) console.log(o.format(Date.now())) // Oct 6, 2020
// Date only with long format o = new Intl.DateTimeFormat('en' , { dateStyle: 'long'}) console.log(o.format(Date.now())) // October 6, 2020 dateStyle 和 timeStyle 选项与不同的语言标记一起使用,如下例所示:
=========== let abc // English language abc = new Intl.DateTimeFormat('en' , { timeStyle: 'short', dateStyle: 'long'}) console.log(abc.format(Date.now())) // October 6, 2020 at 11:40 PM
// Italian language abc = new Intl.DateTimeFormat('it' , { timeStyle: 'short', dateStyle: 'long'}) console.log(abc.format(Date.now())) // 6 ottobre 2020 23:40
// German language abc = new Intl.DateTimeFormat('de' , { timeStyle: 'short', dateStyle: 'long'}) console.log(abc.format(Date.now())) // 6. Oktober 2020 um 23:40 小结 作为开发人员,跟紧语言的新特性是很重要的。如果你错过了 ES2020 的特性更新,建议你阅读这篇文章: