ECMAScript各个版本新增的特性

207 阅读1分钟

ECMAScript 是 JavaScript 语言的标准化规范。以下是各个版本的主要新增特性概述:

ECMAScript 3 (1999)

  • 增加异常处理(trycatchfinally
  • 增加正则表达式支持
  • 增加 in 和 instanceof 运算符
  • 新的控制抽象(switchdo-while

ECMAScript 5 (2009)

  • 严格模式('use strict';
  • 增加了许多数组方法(如 forEachmapfilterreduce 等)
  • 增加 Object.create 和其他对象属性特性
  • 增加 JSON 支持

ECMAScript 6 (2015) - 也被称为 ES2015

  • let 和 const 声明关键字
  • 箭头函数
  • 类(Class)语法
  • 模块导入和导出
  • 解构赋值
  • 增加了许多字符串方法(如 startsWithendsWith 和 includes
  • 模板字面量
  • 默认函数参数
  • 剩余参数和展开操作符(...
  • Promise 和异步编程
  • 生成器(function* 和 yield
  • Map 和 Set 数据结构

ECMAScript 2016 (ES2016)

  • Array.prototype.includes
  • 指数运算符(*

ECMAScript 2017 (ES2017)

  • async 和 await 异步函数
  • Object.entries 和 Object.values
  • String.prototype.padStart 和 String.prototype.padEnd
  • 尾逗号(trailing commas)在函数参数中的支持

ECMAScript 2018 (ES2018)

  • 对象和数组的展开操作符(...
  • 异步迭代器和生成器
  • Promise.prototype.finally
  • RegExp 的改进,如命名捕获组、断言等

ECMAScript 2019 (ES2019)

  • Array.prototype.flatMap 和 Array.prototype.flat
  • Object.fromEntries
  • String.prototype.trimStart 和 String.prototype.trimEnd
  • 可选的 catch 绑定
  • JSON.stringify 的改进,支持稳定的键排序

ECMAScript 2020 (ES2020)

  • BigInt 大整数类型
  • 空值合并运算符(??
  • 可选链运算符(?.
  • import() 动态导入
  • Promise.allSettled
  • globalThis 统一全局对象

ECMAScript 2021 (ES2021)

  • String.prototype.replaceAll
  • Promise.any 和 AggregateError
  • 弱引用(WeakRef
  • FinalizationRegistry用于自定义资源的清理
  • 逻辑赋值运算符(如 ||=&&=??=