ES7
Array.prototype.includes
Includes 方法用来检测数组中是否包含某个元素,返回布尔类型值
// includes indexOfconst mingzhu = ['西游记','红楼梦','三国演义','水浒传'];//判断console.log(mingzhu.includes('西游记')); //trueconsole.log(mingzhu.includes('金瓶梅')); //false
指数操作符 - **
在 ES7 中引入指数运算符「**」,用来实现幂运算,功能与 Math.pow 结果相同
console.log(2 ** 10);// 1024console.log(Math.pow(2, 10));// 1024
ES9
正则表达式命名捕获组
ES9 允许命名捕获组使用符号『?』,这样获取捕获结果可读性更强
不采取命名捕获组
采取命名捕获组
正则表达式反向断言
ES9 支持反向断言(?<=),通过对匹配结果前面的内容进行判断,对匹配进行筛选。
以下列出 ?=、?<=、?!、?<!= 的使用
exp1(?=exp2):查找 exp2 前面的 exp1。
(?<=exp2)exp1:查找 exp2 后面的 exp1。
exp1(?!exp2):查找后面不是 exp2 的 exp1。
(?<!=exp2)exp1:查找前面不是 exp2 的 exp1。
参考链接: 菜鸟正则断言
正则表达式 dotAll 模式
正则表达式中点.匹配除回车外的任何单字符,标记『s』改变这种行为,允许行 终止符出现
//dot . 元字符 除换行符以外的任意单个字符//[\s\S] 匹配所有。\s 是匹配所有空白符,包括换行,\S 非空白符,不包括换行。// ? 匹配前面的子表达式零次或一次,或指明一个非贪婪限定符//* 和 + 限定符都是贪婪的,因为它们会尽可能多的匹配文字,只有在它们的后面//加上一个 ? 就可以实现非贪婪或最小匹配。let str = `<ul><li><a>肖生克的救赎</a><p>上映日期: 1994-09-10</p></li><li><a>阿甘正传</a><p>上映日期: 1994-07-06</p></li></ul>`;//老的声明正则//匹配 <li>+换行+<a>匹配区<\/a>+换行+<p>匹配区</p>//一直要使用换行符\s// const reg = /<li>\s+<a>(.*?)<\/a>\s+<p>(.*?)<\/p>/;//[s]使用 / /s表示允许.匹配所有const reg = /<li>.*?<a>(.*?)<\/a>.*?<p>(.*?)<\/p>/gs;//执行匹配// const result = reg.exec(str);let result;let data = [];//下方使用String.prototype.matchAll代替whilewhile(result = reg.exec(str)){console.log(result)data.push({title: result[1], time: result[2]});}//输出结果console.log(data);
ES11 - String.prototype.matchAll
上边使用while获取result每一项,此处可以使用该方法代替
let str = `<ul><li><a>肖生克的救赎</a><p>上映日期: 1994-09-10</p></li><li><a>阿甘正传</a><p>上映日期: 1994-07-06</p></li></ul>`;//声明正则const reg = /<li>.*?<a>(.*?)<\/a>.*?<p>(.*?)<\/p>/sg//matchAll生产可遍历对象const result = str.matchAll(reg);for(let v of result){console.log(v);}//const arr = [...result];//console.log(arr);
ES10
Object.fromEntries
将二维数组转化为对象
//将二维数组转化为对象const result = Object.fromEntries([['name','尚硅谷'],['xueke', 'Java,大数据,前端,云计算']]);console.log(result); //{name: "尚硅谷", xueke: "Java,大数据,前端,云计算"}//Map + fromEntriesconst m = new Map();m.set('name','ATGUIGU');const result1 = Object.fromEntries(m);console.log(result1); //{name: "ATGUIGU"}//Object.entries ES8//将对象转化为二维数组const arr = Object.entries({name: "尚硅谷"})console.log(arr); //["name", "尚硅谷"]
trimStart 和 trimEnd
// trimlet str = ' iloveyou ';console.log(str.trim());console.log(str.trimStart());console.log(str.trimEnd());
Array.prototype.flat 与 flatMap
//flat 平//将多维数组转化为低位数组// const arr = [1,2,3,4,[5,6]];// const arr = [1,2,3,4,[5,6,[7,8,9]]];//参数为深度 是一个数字// console.log(arr.flat(2));//flatMapconst arr = [1,2,3,4];const result = arr.flatMap(item => [item * 10]);console.log(result);
Symbol.prototype.description
//创建 Symbollet s = Symbol('尚硅谷');console.log(s.description); //尚硅谷
ES11
类的私有属性
class Person{//公有属性name;//私有属性#age;#weight;//构造方法constructor(name, age, weight){this.name = name;this.#age = age;this.#weight = weight;}intro(){console.log(this.name); //晓红console.log(this.#age); //18console.log(this.#weight); //45kg}}//实例化const girl = new Person('晓红', 18, '45kg');// console.log(girl.name);// console.log(girl.#age);//报错// console.log(girl.#weight);//报错girl.intro();
BigInt 大整形
//n表示大整形
// let n = 521n;
// console.log(n, typeof(n));
//函数
// let n = 123;
// console.log(BigInt(n));
// console.log(BigInt(1.2));
//大数值运算
let max = Number.MAX_SAFE_INTEGER;
console.log(max);
console.log(max + 1);
console.log(max + 2);
console.log(BigInt(max))
console.log(BigInt(max) + BigInt(1))
console.log(BigInt(max) + BigInt(2))
globalThis 对象
无论什么开发环境(正常环境、node…)输出的都是全局对象
console.log(globalThis); //Window

