ES6+

作者: 李霖弢 | 来源:发表于2023-10-08 15:47 被阅读0次

    从2015年发布ES2015(即ES6)起,每年都会发布一个新版本。
    有的资料称ES2015后的所有版本统称为ES6,也有的称ES6表示ES2015,ES7表示ES2016以此类推,这里不做讨论。


    ES2015

    • letconst、块级作用域
    • 函数入参默认值、箭头函数(this指向函数定义时的上下文中的this)
    • 一些Number、Math、String、Array、Object方法
    • ` 模板字符串
    • ... 数组展开运算符
    • 解构赋值
    • class
    • ESModel模块化(import、export)
    • Symbol(用于充当永不重复的对象键)
    • Promise
    • Iterator 迭代器(Symbol.iterator、next)
    • Generator 协程(*、yield、next)
    • Proxy 代理器 和 Reffect 反射
    • SetMap
    • WeakSetWeakMap
      其中只能添加对象,且这些对象都是弱引用(不属于正式的引用),不会阻止垃圾回收,即只要没有其他的对象引用该对象,则该对象就会被回收
      因为其中的值任何时候都可能被销毁,所以没必要有迭代能力,也用不着像clear()这样一次性销毁所有值的方法,也没有size属性。

    ES2016

    • ** 指数运算符
    2 ** 10 === Math.pow(2, 10)
    
    • Array.prototype.includes()
      判断一个数组中是否包含指定的值,可解决 indexOf 的 NaN、+0、-0问题
    const arr = [1, 2, 3, 4, 5, NaN]
    console.log(arr.indexOf(NaN)) // -1
    console.log(arr.includes(NaN)) // true
    

    ES2017

    • async/await 语法糖,用于将Promise改写为平级的写法
    • Atomics 对象用于操作 ArrayBuffer
    • Object.values():返回一个给定对象自身的所有可枚举属性值的数组;
    • Object.entries():返回一个给定对象自身可枚举属性的键值对数组;
    • Object.getOwnPropertyDescriptors():返回给定对象所有自有属性的属性描述符。
    • padStart()padEnd() 在字符串首尾添加空格

    ES2018

    • for await...of语句,可遍历异步可迭代对象
    • 正则允许表达式分组命名,s修饰符(启用dotALl模式)、反向断言、反向否定断言
    • ... 对象展开运算符
    • Promise.prototype.finally()

    ES2019

    • Function.prototype.toString():返回的函数体新增了注释与空格;
    • try...catch 语句中的 catch 允许不使用参数
    • trimStart()/trimLeft()trimEnd()/trimRight() 去除字符串左/右的空格
    • Array.prototype.flat() 多维数组降维(传参决定降几维,默认1,可传Infinity)
    const arr = [0, 1, 2, [3, 4]]
    console.log(arr.flat()) // [ 0, 1, 2, 3, 4 ]
    
    • Array.prototype.flatMap() 类似map,但每次回调的返回值是一个数组,最终将各个回调的数组concat到一起作为总的返回值
    [1,2,3].flatMap(item=>[item*item]) // [1, 4, 9]
    
    • Object.fromEntries() 把键值对列表转换为一个对象,是 Object.entries() 的反操作
    • Symbol.prototype.descrption 返回创建Symbol对象时的那个可选的描述字符串
    const sym = Symbol("hello");
    sym.description //hello
    

    ES2020

    • import 动态导入模块
    import('/modules/my-module.js').then(module => {
      // Do something with the module.
    })
    
    • BigInt 数据类型,允许最大数字超过 2**53-1
    • globalThis 指向当前全局对象,在Node中返回 global,在浏览器中返回 window
    • ?? 空值合并运算符
      当左侧的操作数为 null 或者 undefined 时,返回其右侧操作数,否则短路并返回左侧操作数。
      不同于||的假值判断,常用于为空则赋于默认值场景
    const foo = null ?? 'default string';
    console.log(foo);
    // expected output: "default string"
    
    const baz = 0 ?? 42;
    console.log(baz);
    // expected output: 0
    
    • ?. 可选链操作符
      当目标为undefinednull时,短路并返回undefined。后续目标不会继续执行
    obj?.prop
    obj?.[expr]
    arr?.[index]
    func?.(args)
    
    • Promise.allSettled() 返回一个在所有给定的 promise 都已经resolved或rejected后的 promise,并带有一个对象数组,每个对象表示对应的 promise 结果。

    ES2021

    • String.prototype.replaceAll() 全部替换版的 replace
    • _ 数值分隔符,单纯为了方便数字阅读,如11_11===1111
    • WeakRef 允许保留对另一个对象的弱引用,而不会阻止被弱引用对象被GC回收(类似 WeakSet、WeakMap)
    • Promise.any() 不同于Promise.race的总是返回第一个resolve/reject的结果,Promise.any 会尽量返回第一个resolve的结果,但当所有结果都是reject,或者输入数组为空,那么 Promise.any()rejected 包含输入的 promises 执行的 rejection 错误原因集合。
    Promise.any([Promise.reject(1), Promise.reject(2)]).then(res => {
        console.log(res)
    }).catch((e) => {
        console.dir(e)
        console.log(e.errors) // [1, 2]
    })
    
    • &&=||=??=
    const [f1, f2, f3] = [true, false]
    f1 &&= '一碗周' // 等同于 str = str && '一碗周'
    f2 ||= '一碗周' // 等同于 str = str || '一碗周'
    f3 ??= '一碗周' // 等同于 str = str ?? '一碗周'
    

    ES2022

    • 允许在 class 中使用#开头的变量作为类的私有成员
    • 允许在顶层使用await(可以不被 async 函数包裹)
    • Object.hasOwn() 判断对象是否自己有属性(非继承),类似 hasOwnProperty
    • Array.prototype.at() 允许传负值,获取数组中第n项
    [1,2,3].at(-1) // 3
    
    • 正则新增 d 修饰符,execmatch方法额外返回indices属性,表示每各匹配结果在原字符串中的位置
    const text = 'zabbcdef';
    const re = /ab+(cd)/d;
    const result = re.exec(text);
    
    console.log(result[0],result.indices[0]) // abbcd [1,6]
    console.log(result[1],result.indices[1]) // cd [4,5]
    

    ES2023

    • Array.prototype.findLast()find的反向版
    • Array.prototype.findLastIndex()findIndex的反向版
    • Hashbang语法:文件首行#!开头的注释,可用于指定JS解释器路径
    • Symbol 类型允许作为 WeakMap类型的键
    • Array.prototype.toReversed():reserve 的非破坏性版本
    • Array.prototype.toSorted(compareFn):sort 的非破坏性版本
    • Array.prototype.toSpliced(start, deleteCount, ...items):splice 的非破坏性版本
    • Array.prototype.with(index, value):arr[index]=xxx 的非破坏性版本

    相关文章

      网友评论

          本文标题:ES6+

          本文链接:https://www.haomeiwen.com/subject/pnxvbdtx.html