JavaScript语法 ES6、ES7、ES8、ES9、ES10、ES11、ES12新特性汇总

新特性ES6(2015)
1、类(类)
class Man {constructor(name) {this.name = 有课前端网;}console() {console.log(this.name);}}const man = new Man( 有课前端网);man.console(); // 有课前端网
2、智能(ES模块)
// 模块 A 导出一个方法export const sub = (a, b) = a + b;// 模块 B 导入使用import { sub } from ./A;console.log(sub(1, 2)); // 3
3、箭头(箭头)函数
const func = (a, b) = a + b;func(1, 2); // 3
4、函数参数默认值
function foo(age = 25,){ // ...}
5、 模板字符串
const name = 有课前端网;const str = `Your name is ${name}`;
6、解构架构
let a = 1, b= 2;[a, b] = [b, a]; // a 2 b 1
7、延展符
let a = [...hello world]; // [h, e, l, l, o, , w, o, r, l, d]
8、对象属性简写
const name= fly63前端,const obj = { name };
9、承诺
Promise.resolve().then(() = { console.log(2); });console.log(1);// 先打印 1 ,再打印 2
10、让和常量
let name = 有课前端网;const arr = [];
ES7(2016)
1、 Array.prototype.includes()
[1].includes(1); // true
2、指数操作符
2**10; // 1024
ES8(2017)
1、异步/等待
终极终极解决方案
async getData(){const res = await api.getTableData(); // await 异步任务// do something}
2、Object.values()
Object.values({a: 1, b: 2, c: 3}); // [1, 2, 3]
3、 Object.entries()
Object.entries({a: 1, b: 2, c: 3}); // [[a, 1], [b, 2], [c, 3]]
4、字符串填充
// padStarthello.padStart(10); // hello// padEndhello.padEnd(10) hello
5、函数参数列表允许结尾
规定也一样,职能规定与规则和结尾的逗号,保持一致。
6、Object.getOwnPropertyDescriptors()
如果对象的所有自身属性获取一个空,则如果没有返回对象。
7、SharedArrayBuffer对象
/**** @param {*} length 所创建的数组缓冲区的大小,以字节(byte)为单位。* @returns {SharedArrayBuffer} 一个大小指定的新 SharedArrayBuffer 对象。其内容被初始化为 0。*/new SharedArrayBuffer(10)
8、原子对象
Atomics 对象提供了一组静态方法,用于对 SharedArrayBuffer 对象进行原子操作。
ES9(2018)
1、徒劳
await可以和for...的循环使用,以某种方式运行异步操作
async function process(array) {for await (let i of array) {// doSomething(i);}}
2、 Promise.finally()
Promise.resolve().then().catch(e = e).finally();
3、Rest/Spread 属性
const values = [1, 2, 3, 5, 6];console.log( Math.max(...values) ); // 6
4、正则导演组
const reg = /(year[0-9]{4})-(month[0-9]{2})-(day[0-9]{2})/;const match = reg.exec(2021-02-23);
5、正则表达式相反声明
(=p)、(=p) p 前面(位置)、p 后面(位置)(!p)、(!p) 除了 p 前面(位置)、除了 p 后面(位置)
6、正则表达式dotAll模式
正则表达式中点。除匹配回车外的任何单字符,标记改变这种行为,允许行终止符的出现。
/hello.world/.test(hello\nworld); // falseES10(2019)
1、Array.flat()和Array.flatMap()
平坦的()
[1, 2, [3, 4]].flat(Infinity); // [1, 2, 3, 4]
平面图()
[1, 2, 3, 4].flatMap(a = [a**2]); // [1, 4, 9, 16]
2、String.trimStart()和String.trimEnd()
去除字符串首尾字符
3、String.prototype.matchAll
const raw_arr = test1 test2 test3.matchAll((/t(e)(st(\d))/g));const arr = [...raw_arr];
4、Symbol.prototype.description
Symbol(description).description; // description
5、 Object.fromEntries()
// 通过 Object.fromEntries, 可以将 Map 转化为 Object:const map = new Map([ [foo, bar], [baz, 42] ]);console.log(Object.fromEntries(map)); // { foo: bar, baz: 42 }
6、任选捕获
在 ES10 之前,我们是这样异常的监护人:
try {// tryCode} catch (err) {// catchCode}
这里的err是必须的参数,在ES10可以省略这个参数。
ES11(2020)
1、空值合并操作符(空值处理)
表达式在的左边求价值,未定义或返回其性质。
let user = {u1: 0,u2: false,u3: null,u4: undefinedu5: ,}let u2 = user.u2 用户2 // falselet u3 = user.u3 用户3 // 用户3let u4 = user.u4 用户4 // 用户4let u5 = user.u5 用户5 //
2、可选链(任选链)
用户检测宝宝的孩子
let user = {}let u1 = user.childer.name // TypeError: Cannot read property name of undefinedlet u1 = user.childer.name // undefined
3、Promise.allSettled
const promise1 = Promise.resolve(3);const promise2 = 42;const promise3 = new Promise((resolve, reject) = reject(我是失败的Promise_1));const promise4 = new Promise((resolve, reject) = reject(我是失败的Promise_2));const promiseList = [promise1,promise2,promise3, promise4]Promise.allSettled(promiseList).then(values={console.log(values)});
4、进口()
导入导入
5、新基本数据类型 BigInt
任意匹配的概率
6、globalThis
浏览器:window
工人:自己
节点:全局
ES12(2021)
1、全部替换
const str = hello world;str.replaceAll(l, ); // heo word
2、Promise.any
const promise1 = new Promise((resolve, reject) = reject(我是失败的Promise_1));const promise2 = new Promise((resolve, reject) = reject(我是失败的Promise_2));const promiseList = [promise1, promise2];Promise.any(promiseList).then(values={console.log(values);}).catch(e={console.log(e);});
3、弱引用
4、逻辑和形式表现
a ||= b//等价于a = a || (a = b)a = b//等价于a = a (a = b)a = b//等价于a = a (a = b)
5、数字字符
数字间隔性符,可以在数字之间创建可视化分隔符,通过_下划线来分割数字,使数字化可计算。
const money = 1_000_000_000;//等价于const money = 1000000000;1_000_000_000 === 1000000000; // true
本文完~
学习更多技能
请点击下方公众号
![]()

评论
