当前位置:   article > 正文

ES7、ES8、ES9、ES10、ES11 新特性 总结_前端es7

前端es7

目录

ES7新特性

一、Array.prototype.includes

二、指数操作符

ES8新特性

一、async 和 await

1.async 函数

2.await 表达式

async和await结合读取文件:

二、Object.values 和 Object.entries

ES9新特性

一、Rest/Spread 属性

二、正则表达式命名捕获组

三、正则表达式反向断言

        四、正则表达式 dotAll 模式

ES10新特性

一、Object.fromEntries

二、trimStart 和 trimEnd

三、Array.prototype.flat 与 flatMap

四、Symbol.prototype.description

ES11新特性

一、String.prototype.matchAll

二、类的私有属性

三、Promise.allSettled

四、可选链操作符

五、动态 import 导入

六、globalThis 对象

七、BigInt 


ES7新特性

一、Array.prototype.includes

        Includes 方法用来检测数组中是否包含某个元素,返回布尔类型值
  1. const name = ['Tom','Jerry','Bob']
  2. //判断
  3. console.log(name.includes("Tom")); //true
  4. console.log(name.includes("Jack")); //false

二、指数操作符

ES7 中引入指数运算符「 ** 」,用来实现幂运算,功能与 Math.pow 结果相同
  1. console.log(2 ** 10); //1024
  2. console.log(Math.pow(2,10)); //1024

ES8新特性

一、async await

        async 和 await 两种语法结合可以让异步代码像同步代码一样

1.async 函数

  1. async 函数的返回值为 promise 对象,
  2. promise 对象的结果由 async 函数执行的返回值决定
  1. async function fn(){
  2. //return "Tom"
  3. //throw new Error("出错了!")//抛出错误,返回的结果是一个失败的Promise
  4. //返回的结果如果是一个Promise对象
  5. return new Promise((resolve,reject)=>{
  6. resolve('成功的数据')
  7. //reject('失败的错误')
  8. })
  9. }
  10. const result = fn()
  11. //调用then方法
  12. result.then(value=>{
  13. console.log(value);
  14. },reason=>{
  15. console.warn(reason);
  16. })
  17. console.log(result);

 

2.await 表达式

  1. await 必须写在 async 函数中
  2. await 右侧的表达式一般为 promise 对象
  3. await 返回的是 promise 成功的值
  4. await promise 失败了, 就会抛出异常, 需要通过 try...catch 捕获处理

async和await结合读取文件:

  1. //1.引入fs模块
  2. const fs = require("fs")
  3. //读取【01】
  4. function read01(){
  5. return new Promise((resolve,reject)=>{
  6. fs.readFile("./resources/01.md",(err,data)=>{
  7. //如果失败
  8. if(err) reject(err);
  9. //如果成功
  10. resolve(data)
  11. })
  12. })
  13. }
  14. //读取【02】
  15. function read02(){
  16. return new Promise((resolve,reject)=>{
  17. fs.readFile("./resources/02.md",(err,data)=>{
  18. //如果失败
  19. if(err) reject(err);
  20. //如果成功
  21. resolve(data)
  22. })
  23. })
  24. }
  25. //读取【01】
  26. function read03(){
  27. return new Promise((resolve,reject)=>{
  28. fs.readFile("./resources/03.md",(err,data)=>{
  29. //如果失败
  30. if(err) reject(err);
  31. //如果成功
  32. resolve(data)
  33. })
  34. })
  35. }
  36. //声明一个async函数
  37. async function main(){
  38. try{
  39. //获取01内容
  40. let read1 = await read01()
  41. //获取02内容
  42. let read2 = await read02()
  43. //获取03内容
  44. let read3 = await read03()
  45. console.log(read1.toString());
  46. console.log(read2.toString());
  47. console.log(read3.toString());
  48. }catch(e){
  49. console.log(e);
  50. }
  51. }
  52. main()

二、Object.values Object.entries

1. Object.values() 方法返回一个给定对象的所有可枚举属性值的数组
2. Object.entries() 方法返回一个给定对象自身可遍历属性 [key,value] 的数组
3. Object.getOwnPropertyDescriptors 该方法返回指定对象所有自身属性的描述对象
  1. const person = {
  2. name:"Tom",
  3. hobby:['basketball','dance','rap'],
  4. subject:['Math','English']
  5. }
  6. //获取对象所有的键
  7. console.log(Object.keys(person));
  8. //获取对象所有的值
  9. console.log(Object.values(person));
  10. //entries
  11. console.log(Object.entries(person));
  12. //创建Map
  13. const m = new Map(Object.entries(person))
  14. console.log(m);
  15. console.log(m.get('hobby'));
  16. //对象属性的描述对象
  17. console.log(Object.getOwnPropertyDescriptors(person));

 

ES9新特性

一、Rest/Spread 属性

        Rest 参数与 spread 扩展运算符在 ES6 中已经引入,不过 ES6 中只针对于数组,在 ES9 中为对象提供了像数组一样的 rest 参数和扩展运算符

例一: 

  1. function connect({host,port,...user}){
  2. console.log(host);
  3. console.log(port);
  4. console.log(user);
  5. }
  6. connect({
  7. host:'127.0.0.1',
  8. port:3306,
  9. username:"root",
  10. password:"root",
  11. type:"master"
  12. })

例二:

  1. const one = {
  2. a:"aa"
  3. }
  4. const two = {
  5. b:"bb"
  6. }
  7. const three = {
  8. c:"cc"
  9. }
  10. const all = {...one,...two,...three}
  11. console.log(all);

二、正则表达式命名捕获组

        ES9 允许命名捕获组使用符号『 ?<name> , 这样获取捕获结果可读性更强

一般方式:

  1. //声明一个字符串
  2. let str = "<a href='http://www.baidu.com'>百度</a>"
  3. //提取url与【标签文本】
  4. const reg = /<a href='(.*)'>(.*)<\/a>/;
  5. //执行
  6. const result = reg.exec(str)
  7. console.log(result);
  8. console.log(result[1]);
  9. console.log(result[2]);

 

 命名捕获组:

  1. let str = "<a href='http://www.baidu.com'>百度</a>"
  2. const reg = /<a href='(?<url>.*)'>(?<text>.*)<\/a>/;
  3. const result = reg.exec(str)
  4. console.log(result);
  5. console.log(result.groups.url);
  6. console.log(result.groups.text);

 

三、正则表达式反向断言

        ES9 支持反向断言,通过对匹配结果前面的内容进行判断,对匹配进行筛选。
正向断言:
  1. let str = "js234566酷酷酷222啦啦啦"
  2. //正向断言
  3. const reg = /\d+(?=啦)/
  4. console.log(reg.exec(str));

反向断言:

  1. let str = "js234566酷酷酷222啦啦啦"
  2. //反向断言
  3. const reg = /(?<=酷)\d+/
  4. console.log(reg.exec(str));

 

四、正则表达式 dotAll 模式

正则表达式中点 . 匹配除回车外的任何单字符,标记『s』改变这种行为,允许行终止符出现
 
  1. //dot . 元字符 除换行符以外的任意单个字符
  2. let str = `
  3. <ul>
  4. <li>
  5. <a>肖生克的救赎</a>
  6. <p>上映日期: 1994-09-10</p>
  7. </li>
  8. <li>
  9. <a>阿甘正传</a>
  10. <p>上映日期: 1994-07-06</p>
  11. </li>
  12. </ul>`;
  13. const reg = /<li>.*?<a>(.*?)<\/a>.*?<p>(.*?)<\/p>/gs
  14. let result;
  15. let data = []
  16. while(result = reg.exec(str)){
  17. data.push({title:result[1],time:result[2]})
  18. }
  19. console.log(data);

 

ES10新特性

一、Object.fromEntries

        在ES8中使用Object.entries将对象转化为数组,在ES10中,使用Object.fromEntries将数组转换为对象。

  1. const result = Object.fromEntries([
  2. ['name',"Tom"],
  3. ['hobby','dance,rap']
  4. ])
  5. console.log(result);
  6. //Map
  7. const m = new Map();
  8. m.set('name','Tom')
  9. const result1 = Object.fromEntries(m)
  10. console.log(result1);
  11. //在ES8中
  12. const arr = Object.entries({
  13. name:"Tom"
  14. })
  15. console.log(arr);

二、trimStart trimEnd

        trimStart清除字符串左侧空白;trimEnd清除字符串右侧空白 

  1. let str = " kkk "
  2. console.log(str); //' kkk '
  3. console.log(str.trimStart()); //'kkk '
  4. console.log(str.trimEnd()); //' kkk'

三、Array.prototype.flat flatMap

Array.prototype.flat将多维数组转化为低维数组,参数为深度,是一个数字

  1. const arr = [1,2,[3,4,5]]
  2. console.log(arr.flat());
  3. const arr1 = [1,2,[3,4,[5,6]]]
  4. console.log(arr1.flat(2));
  5. //flatMap
  6. const arr2 = [1,2,3,4]
  7. const result = arr2.flatMap(item => [item*10])
  8. console.log(result);

四、Symbol.prototype.description

获取Symbol的字符串描述

  1. let s = Symbol("Tom")
  2. console.log(s.description); //Tom

ES11新特性

一、String.prototype.matchAll

 String.prototype.matchAll得到正则批量匹配的结果

  1. let str = `
  2. <ul>
  3. <li>
  4. <a>肖生克的救赎</a>
  5. <p>上映日期: 1994-09-10</p>
  6. </li>
  7. <li>
  8. <a>阿甘正传</a>
  9. <p>上映日期: 1994-07-06</p>
  10. </li>
  11. </ul>`;
  12. //声明正则
  13. const reg = /<li>.*?<a>(.*?)<\/a>.*?<p>(.*?)<\/p>/sg
  14. //调用方法
  15. const result = str.matchAll(reg)
  16. const arr = [...result]
  17. console.log(arr);

 

二、类的私有属性

  1. class Person{
  2. //公有属性
  3. name;
  4. //私有属性
  5. #age;
  6. #weight;
  7. //构造方法
  8. constructor(name,age,weight){
  9. this.name = name;
  10. this.#age = age;
  11. this.#weight = weight
  12. }
  13. intro(){
  14. console.log(this.name);
  15. console.log(this.#age);
  16. console.log(this.#weight);
  17. }
  18. }
  19. //实例化
  20. const girl = new Person('Tom',12,"45KG")
  21. //console.log(girl.#age); //获取不到
  22. girl.intro()

 

三、Promise.allSettled

接收一个Promise数组,返回一个Promise对象,返回结果永远是成功的状态

  1. //声明两个Promise对象
  2. const p1 = new Promise((resolve,reject)=>{
  3. setTimeout(()=>{
  4. resolve('商品数据 - 1')
  5. },1000)
  6. })
  7. const p2 = new Promise((resolve,reject)=>{
  8. setTimeout(()=>{
  9. //resolve('商品数据 - 2')
  10. reject('出错了')
  11. },1000)
  12. })
  13. //调用allSettled方法
  14. const result = Promise.allSettled([p1,p2])
  15. console.log(result);

 

四、可选链操作符

         ?. 在对象层级较深时,不用做层级判断的简便写法

  1. function main(config){
  2. //const cache1Host = config && config.cache1 &&config.cache1.host
  3. const cache1Host = config?.cache1?.host
  4. console.log(cache1Host);
  5. }
  6. main({
  7. cache1:{
  8. host:'192.168.1.100',
  9. username:'root'
  10. },
  11. cache2:{
  12. host:'192.168.1.200',
  13. username:'admin'
  14. }
  15. })

五、动态 import 导入

        实现按需加载

 

 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <button id="btn">点一下</button>
  9. </body>
  10. <script src="./app.js" type="module"></script>
  11. </html>

 

六、globalThis 对象

        不管在任何环境下,globalThis始终指向全局对象

console.log(globalThis);    //Window

 

七、BigInt 

        大整型,应用于大数值运算

  1. let n = 123n
  2. console.log(n,typeof(n));//123n "bigint"
  3. //函数
  4. let a = 123
  5. console.log(BigInt(a));//123n
  1. //大数值运算
  2. let max = Number.MAX_SAFE_INTEGER;
  3. console.log(max); //9007199254740991
  4. console.log(max+1);//9007199254740992
  5. console.log(max+2);//9007199254740992
  6. console.log(BigInt(max)); //9007199254740991n
  7. console.log(BigInt(max)+BigInt(1));//9007199254740992n
  8. console.log(BigInt(max)+BigInt(2));//9007199254740993n

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号