ECMAScript 2021 最新更新

2025-06-04

ECMAScript 2021 最新更新

简介😎

ECMAScript 是 JavaScript 语言的一部分,主要用于 Web 技术、网站或 Web 应用的构建。ECMAScript 正在发展成为世界上使用最广泛的通用编程语言之一。它主要用于嵌入 Web 浏览器,也适用于服务器和嵌入式应用程序。

ECMAScript 的新更新将于今年 7 月发布。这些新的改进旨在增强 JavaScript 的功能,并简化开发人员的工作。它提供了新功能、简化复杂工作的方法等等。
欧洲汽车制造商协会

最新更新🤩

ECMAScript 2021 中的新 JavaScript 特性包括:
1. 逻辑赋值运算符
And & Equals (&&=)
OR & Equals (||=)
Nullish Coalescing & Equals (??=)
2. 数字分隔符
3. String replaceAll
4. Promise.any
5. 私有类方法
6. 私有 Getter 和 Setter

1. 逻辑赋值运算符
逻辑赋值运算符引入了将逻辑运算符和赋值表达式结合起来的新运算符。

与 & 等于 (&&=)
当值为真时进行赋值。

以前的 :

let x = 1;
if(x){
  x = 10;
}
// Output: x = 10
Enter fullscreen mode Exit fullscreen mode

新的 :

let x = 1;
x &&= 10
// Output: x = 10
Enter fullscreen mode Exit fullscreen mode

或 & 等于 (||=)
当值为假时分配。

以前的 :

let x = 0;
x = x || 10;
// Output: x = 10
Enter fullscreen mode Exit fullscreen mode

新的 :

let x = 0;
x ||= 10
// Output: x = 10
Enter fullscreen mode Exit fullscreen mode

解释:仅当 x 为假值时,才会发生赋值操作。如果 x 包含 1(真值),则不会发生赋值。此处 x 包含 0,因此会发生赋值。

空值合并 & 等于 (??=)
符号 ?? 是 JavaScript 中的空值合并运算符。它检查值是否为空或未定义。

let x;
let y = 10;
x ??= y;
Enter fullscreen mode Exit fullscreen mode

x 的值未定义,因此对右侧表达式进行求值并将 x 设置为 10。

2. 数字分隔符
为了提高可读性并分隔数字组,数字文字使用下划线作为分隔符。

// A billion dollar that I want to earn
const money = 1_000_000_000;
const money = 1_000_000_000.00;
// Also can be used for Binary, Hex, Octal bases
Enter fullscreen mode Exit fullscreen mode

js

3. 字符串 replaceAll
如果我们想替换字符串中子字符串的所有实例,那么这个新方法 replaceAll 非常有用。

const s = "You are reading JavaScript 2021 new updates.";
console.log(s.replaceAll("JavaScript", "ECMAScript"));
// output : You are reading ECMAScript 2021 new updates.
Enter fullscreen mode Exit fullscreen mode

4. Promise.any
Promise.any() 方法返回一个 Promise,只要其中一个 Promise 解析成功,该 Promise 就会解析成功。它与 Promise.all() 方法相反,后者会等待所有 Promise 解析成功后再解析。

等等,当所有 Promise 都被拒绝时会发生什么?是的,你懂的,该方法会抛出一个 AggregateError 异常,并显示拒绝原因。我们必须把代码放在 try-catch 块里。

const promiseOne = new Promise((resolve, reject) => {
  setTimeout(() => reject(), 1000);
});
const promiseTwo = new Promise((resolve, reject) => {
  setTimeout(() => reject(), 2000);
});
const promiseThree = new Promise((resolve, reject) => {
  setTimeout(() => reject(), 3000);
});
try {
  const first = await Promise.any([
    promiseOne, promiseTwo, promiseThree
  ]);
  // If any of the promises was satisfied.
} catch (error) {
  console.log(error);
  // AggregateError: If all promises were rejected
}
Enter fullscreen mode Exit fullscreen mode

5. 私有类方法

私有方法仅在类内部具有作用域,因此在类外部无法访问它们,请参见此示例

以前的 :

class Me{
  showMe() {
    console.log("I am a programmer")
  }
  #notShowMe() {
    console.log("Hidden information")
  }
}
const me = new Me()
me.showMe()
me.notShowMe()
//error
Enter fullscreen mode Exit fullscreen mode

这段代码会抛出一个错误,提示“gfg.notShowMe 不是一个函数”。这是因为 #notShowMe() 现在是 GfG 类内部的一个私有方法,只能通过类内部的公共方法访问。

新的 :

class Me {
  showMe() {
    console.log("I am a programmer");
  }
  #notShowMe() {
    console.log("Hidden information");
  }
  showAll() {
    this.showMe()
    this.#notShowMe();
  }
}
const me = new Me();
me.showAll();
//I am a programmer
//Hidden information
Enter fullscreen mode Exit fullscreen mode

现在我们在类 Me 中创建一个名为 showAll() 的新公共方法,通过这个公共方法,我们可以访问私有方法 #notShowMe(),并且由于我们的新方法是公共的,所以我们得到这个输出。

6. 私有 Getter 和 Setter
就像私有方法一样,现在我们可以将 Getter 和 Setter 设为私有,以便只能在类内部或通过创建的实例访问它们。

class Me {
  get #Name() {
    return "Animesh"
  }
  get viewName() {
    return this.#Name
  }
}
let name = new Me();
console.log(name.viewName)
// Output: Animesh
Enter fullscreen mode Exit fullscreen mode

结论
JavaScript 是最流行的语言之一,这些频繁更新的新功能使其更加出色,也更易于开发。所以,欢迎所有这些新功能。

阅读
C++ 已删除的功能

连接
LinkedIn
Twitter

更新棒极了!😊

文章来源:https://dev.to/animeshmaru/ecmascript-2021-new-updates-1ie9
PREV
害怕在会议上发言?那就提交吧!
NEXT
高阶数组函数 forEach、map 和 filter