ES6+

2023-10-08  本文已影响0人  李霖弢

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


ES2015


ES2016

2 ** 10 === Math.pow(2, 10)
const arr = [1, 2, 3, 4, 5, NaN]
console.log(arr.indexOf(NaN)) // -1
console.log(arr.includes(NaN)) // true

ES2017


ES2018


ES2019

const arr = [0, 1, 2, [3, 4]]
console.log(arr.flat()) // [ 0, 1, 2, 3, 4 ]
[1,2,3].flatMap(item=>[item*item]) // [1, 4, 9]
const sym = Symbol("hello");
sym.description //hello

ES2020

import('/modules/my-module.js').then(module => {
  // Do something with the module.
})
const foo = null ?? 'default string';
console.log(foo);
// expected output: "default string"

const baz = 0 ?? 42;
console.log(baz);
// expected output: 0
obj?.prop
obj?.[expr]
arr?.[index]
func?.(args)

ES2021

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

[1,2,3].at(-1) // 3
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

上一篇下一篇

猜你喜欢

热点阅读