ES11
版本记录
版本介绍
来源: https://262.ecma-international.org/11.0/ Introduction
This specification, the 11th edition, introduces the matchAll
method for Strings, to produce an iterator for all match objects generated by a global regular expression; import()
, a syntax to asynchronously import Modules with a dynamic specifier; BigInt
, a new number primitive for working with arbitrary precision integers; Promise.allSettled
, a new Promise combinator that does not short-circuit; globalThis
, a universal way to access the global this value; dedicated export * as ns from 'module' syntax for use within modules; increased standardization of for-in enumeration order; import.meta
, a host-populated object available in Modules that may contain contextual information about the Module; as well as adding two new syntax features to improve working with “nullish” values (null or undefined): nullish coalescing, a value selection operator; and optional chaining, a property access and function invocation operator that short-circuits if the value to access/invoke is nullish.
版本概要
- String.prototype.matchAll
- import()
- BigInt
- Promise.allSettled
- globalThis
- for-in mechanics
- 可选链操作符 ?.
- 空值合并运算符 ??
- import.meta
版本特性
String.prototype.matchAll()
matchAll()
方法返回一个迭代器,该迭代器包含了检索字符串与正则表达式进行匹配的所有结果(包括捕获组)。
const regexp = /t(e)(st(\d?))/g
const str = 'test1test2'
const array = [...str.matchAll(regexp)]
console.log(array[0])
// Expected output: Array ["test1", "e", "st1", "1"]
console.log(array[1])
// Expected output: Array ["test2", "e", "st2", "2"]
Promise.allSettled()
Promise.allSettled()
静态方法将一个 Promise
可迭代对象作为输入,并返回一个单独的 Promise
。当所有输入的 Promise
都已敲定时(包括传入空的可迭代对象时),返回的 Promise 将被兑现,并带有描述每个 Promise 结果的对象数组。
const promise1 = Promise.resolve(3)
const promise2 = new Promise((resolve, reject) =>
setTimeout(reject, 100, 'foo')
)
const promises = [promise1, promise2]
Promise.allSettled(promises).then((results) =>
results.forEach((result) => console.log(result.status))
)
// Expected output:
// "fulfilled"
// "rejected"
BigInt
BigInt
是一种内置对象,它提供了一种方法来表示大于 2^53 - 1
的整数。这原本是 Javascript
中可以用 Number
表示的最大数字。BigInt
可以表示任意大的整数。
可以用在一个整数字面量后面加 n
的方式定义一个 BigInt ,如:10n
,或者调用函数 BigInt()
(但不包含 new 运算符)并传递一个整数值或字符串值。
const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER);
// ↪ 9007199254740991n
const maxPlusOne = previousMaxSafe + 1n;
// ↪ 9007199254740992n
const theFuture = previousMaxSafe + 2n;
// ↪ 9007199254740993n, this works now!
const multi = previousMaxSafe * 2n;
// ↪ 18014398509481982n
const subtr = multi – 10n;
// ↪ 18014398509481972n
const mod = multi % 10n;
// ↪ 2n
const bigN = 2n ** 54n;
// ↪ 18014398509481984n
bigN * -1n
// ↪ –18014398509481984n
globalThis
全局属性 globalThis
包含全局的 this
值,类似于全局对象(global object)。
import()
import.meta
import.meta
元属性将特定上下文的元数据暴露给 JavaScript
模块。它包含了这个模块的信息,例如这个模块的 URL
。
可选链操作符(?.
)
可选链运算符(?.)
允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。?.
运算符的功能类似于 .
链式运算符,不同之处在于,在引用为空 (nullish ) (null 或者 undefined)
的情况下不会引起错误
,该表达式短路返回值是 undefined
。与函数调用一起使用时,如果给定的函数不存在,则返回 undefined
。
当尝试访问可能不存在的对象属性时,可选链运算符将会使表达式更短、更简明。在探索一个对象的内容时,如果不能确定哪些属性必定存在,可选链运算符也是很有帮助的。
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah',
},
}
const dogName = adventurer.dog?.name
console.log(dogName)
// Expected output: undefined
console.log(adventurer.someNonExistentMethod?.())
// Expected output: undefined
空值合并运算符(??
)
空值合并运算符(??
)是一个逻辑运算符,当左侧的操作数为 null
或者 undefined
时,返回其右侧操作数,否则返回左侧操作数。
与逻辑或运算符(||
)不同,逻辑或运算符会在左侧操作数为假值时返回右侧操作数。也就是说,如果使用 ||
来为某些变量设置默认值,可能会遇到意料之外的行为。比如为假值(例如,''
或 0
)时。见下面的例子。
const foo = null ?? 'default string'
console.log(foo)
// Expected output: "default string"
const baz = 0 ?? 42
console.log(baz)
// Expected output: 0