目录
包含ES6、ES7、ES8、ES9、ES10、ES11新特性
ECMAScript7新特性
-
Array.prototype.includes
includes方法用于检测数组中是否包含某个元素,返回布尔类型值
-
指数运算符
在ES7中引入指数运算符“**”,用来实现幂运算,功能与Math.pow结果相同
ECMAScript8新特性——Async、Await
链接:ES2015+学习笔记系列(十)——Async和Await
ES8对象方法扩展
-
Object.values()方法返回一个给定对象的所有可枚举属性值的数组
-
Object.entries()方法返回一个给定对象自身可遍历属性[key,value]的数组
-
Object.getOwnPropertyDescriptors()方法返回指定对象所有自身属性的描述对象
const es8Obj = {
url:'2.2.2.2',
method:'get',
host:'localhost'
}
console.log(Object.keys(es8Obj));
console.log(Object.values(es8Obj));
console.log(Object.entries(es8Obj));
console.log(Object.getOwnPropertyDescriptors(es8Obj));
ES9 扩展运算符与rest参数
- Rest参数与spread扩展运算符在ES6中已经引入,不过ES6中只针对数组。
- 在ES9在为对象提供了像数组一样的rest参数和扩展运算符
ES9正则扩展
命名捕获分组
let url = '<a href="https:baidu.com">百度</a>';
let reg = /<a href="(.*)">(.*)<\/a>/;
let result = reg.exec(url);
console.log(result);
console.log(result[0]);
console.log(result[1]);
console.log(result[2]);
let reg2 = /<a href="(?<str>.*)">(?<name>.*)<\/a>/;
let result2 = reg2.exec(url);
console.log(result2);
console.log(result2.groups.str);
console.log(result2[1]);
反向断言
根据目标前后内容做唯一性识别
let str = "JS1234你知道么555啦啦啦";//匹配555
let reg = /\d+(?=啦)/;
let result = reg.exec(str);
console.log(result);//555
//反向断言
let reg2 = /(?<=么)\d+/
console.log(reg2.exec(str));//555
dotAll模式
dot . 元字符–>匹配处换行符之外的任意字符
dotAll模式使.可以匹配任意字符
ES10 新特性
Object.fromEntries
- 传入二维数组,创建对象
let arr = [['a','es10学习'],['b','es10对象扩展']];
let es10Obj = Object.fromEntries(arr);
console.log(es10Obj);//{ a: 'es10学习', b: 'es10对象扩展' }
- 传入Map
let map = new Map();
map.set("name", "aaa");
let e = Object.fromEntries(map);
console.log(e); //{name:'aaa'}
//fromEntries与entries逆操作
console.log(Object.entries(e)); //['name','aaa']
trimStart与trimEnd
用以清除字符前后的空白
flat与flatMap
数组的两个方法
- flat将多为数组转换为低维数组,参数为整数,表示深度,默认值为1
- flatMap()数组的方法,与Array.map类似,可以将遍历的item返回的结果降低维度
Symbol.prototype.description
获取Symbol函数参数值
ES11新特性
私有属性
对属性的封装,私有属性用“#”标识
class Person{
//公有属性
name;
//私有属性
#age;
#weight;
constructor(name,age,weight){
this.name = name;
this.#age = age;
this.#weight = weight;
}
intro(){
console.log(this.#age);
}
}
let person = new Person('aa',13,22)
console.log(person.intro());
// console.log(person.#age);//报错
Promise批处理方法
- Promise.allSettled()
- 接收Promise数组,返回Promise对象
- 返回的结果永远是成功的状态,结果为每一个promise状态和结果值
- Promise.all()
- 接收Promise数组
- 返回结果由数组中每一个promise状态决定,全成功才成功
字符串扩展方法matchAll
String.prototype.matchAll用来得到正则批量匹配得到的结果
可选链操作符
-
形式:“?.”的组合
-
对象类型参数层级比较深,此符号可避免层层判断
动态import
实现按需加载
import()函数,传入资源路径,返回结果为promise对象
BigInt
用于大整数数值运算
- 在普通数字后加一个n即为大整型
- 转换为此类型方法BigInt(),只能传入整数
- 不能与普通整数做运算,需要把普通整数转换为BigInt类型
globalThis
全局的this,始终指向全局对象