# ES11 ⭐️
# 私有属性
简单来说就是在class
里面的属性前面加一个#
就和c++里面的private
一样
不过多阐述了!
# Promise.allSettled
解决了Promise.all,有一个坏了就全坏了的情况!
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:
// { status: 'fulfilled', value: 3 }
// { status: 'rejected', reason: 'foo' }
# String.prototype.matchAll
matchAll()
方法返回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器,这样就可以使用of
迭代了
let str = `<ul>
<li>
<a>123</a>
<p>456</p>
</li>
<li>
<a>789</a>
<p>987</p>
</li>
</ul>`
const reg = /<li>.*?<a>(?<texta>.*?)<\/a>.*?<p>(?<textp>.*?)<\/p>/sg
const result = str.matchAll(reg)
for (let v of result) {
console.log(v.groups.texta);
console.log(v.groups.textp);
}
# 可选链操作符?.
?.
让我们不用去判断是否存在了
方便我们的链式调用
// ES2019的写法
function doSomething(onContent, onError) {
try {
// ... do something with the data
}
catch (err) {
if (onError) { // 校验onError是否真的存在
onError(err.message);
}
}
}
/******************************************/
// 使用可选链进行函数调用
function doSomething(onContent, onError) {
try {
// ... do something with the data
}
catch (err) {
onError?.(err.message); // 如果onError是undefined也不会有异常
}
}
# 空值合并运算符??
空值合并操作符(??)是一个逻辑操作符,当左侧的操作数为 null 或者 undefined 时,返回其右侧操作数,否则返回左侧操作数。
let res1 = 0 ?? 42
let res2 = 0 || 42
console.log(res1);
console.log(res2);
# BigInt类型
BigInt 是一种内置对象,它提供了一种方法来表示大于 2^53 - 1 的整数。这原本是 Javascript中可以用 Number 表示的最大数字。BigInt 可以表示任意大的整数。
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)。
← ES10 Javascript面试题1 →