【JavaScript Weekly #570】ES2021 新特性!

🥳 欢迎有兴趣的小伙伴,一起做点有意义的事!本文译者:

https://github.com/daodaolee

我发起了一个周刊翻译计划,仓库地址:

https://github.com/FEDarling

,访问地址:

https://fedarling.github.io/fe-translate-weekly/

现在还很缺志同道合的小伙伴,纯属个人兴趣,当然对于提升英语和前端技能也会有帮助,要求:英语不要差的离谱、github熟练使用、有恒心、谦虚、对自己做的事负责。

想参与的小伙伴,可以

https://cdn.jsdelivr.net/gh/daodaolee/photobed/img/202111162148554.jpg

联系我,也可以给仓库发issue留言,我博客也有具体的个人联系方式:

https://daodaolee.cn/

逻辑赋值操作符(&&= ||= ??=)

逻辑赋值操作符

https://github.com/tc39/proposal-logical-assignment

.

// 或 或等 x ||= y; x || (x = y); // 与 与等 x &&= y; x && (x = y); // 零合并 x ??= y; x ?? (x = y);
const updateID = user => { // 可以这样写 if (!user.id) user.id = 1 // 或者这样 user.id = user.id || 1 // 或者用逻辑赋值运算符 user.id ||= 1 }
function setOpts(opts) { opts.cat ??= 'meow' opts.dog ??= 'bow'; } setOpts({ cat: 'meow' })

数字分隔符

数字组合之间的分隔

https://github.com/tc39/proposal-numeric-separator

.

1_000_000_000 // 啊,十亿 101_475_938.38 // 这是好多亿 let fee = 123_00; // 123 美元 let fee = 12_300; // 12,300 美元 let amount = 12345_00; // 12,345 let amount = 123_4500; // 123.45 let amount = 1_234_500; // 1,234,500
0.000_001 // 百万分之1 1e10_000 // 10^10000 0xA0_B0_C0;

Promise.any 和错误合集

Promise.any + 错误合集

https://github.com/tc39/proposal-promise-any

.

Promise.any([ fetch('https://v8.dev/').then(() => 'home'), fetch('https://v8.dev/blog').then(() => 'blog'), fetch('https://v8.dev/docs').then(() => 'docs') ]).then((first) => { // 任意一个 promise 状态为 fulfilled console.log(first); // → 'home' }).catch((error) => { // 所有 promise 状态都为 rejected console.log(error); });

String.prototype.replaceAll

替换所有

https://github.com/tc39/proposal-string-replaceall

.

// String.prototype.replaceAll(searchValue, replaceValue) 'x'.replace('', '_'); // → '_x' 'xxx'.replace(/(?:)/g, '_'); // → '_x_x_x_' 'xxx'.replaceAll('', '_'); // → '_x_x_x_'

WeakRefs 和 FinalizationRegistry 对象

WeakRefs 和 FinalizationRegistry

https://github.com/tc39/proposal-weakrefs

let target = {}; let wr = new WeakRef(target); //wr 和 target 不是同一个,wr是弱引用 // 创建一个新的注册者 const registry = new FinalizationRegistry(heldValue => { // .... }); registry.register(myObject, "some value", myObject); // 执行一些逻辑处理之后,如果你不再使用 `myObject`,可以解绑 registry.unregister(myObject);

相关链接

https://h3manth.com/ES2021/

https://github.com/FEDarling/fe-translate-weekly/blob/master/JavaScript_Weekly/570/ES2021_new_feature.md