ES13-ES14新特性一览

ES13新特性

ES13新增语法
  1.at函数 可以使用at函数来索引元素,支持数组和字符串
    传正数从前往后找,下标从0开始。传负数从后往前找,下标从-1开始
    const arr = ['a', 'b', 'c', 'd'];
    // 第一个元素
    console.log(arr.at(0)); // a
    // 倒数第一个元素
    console.log(arr.at(-1)); // d
    // 倒数第二个元素
    console.log(arr.at(-2)); // c

    const str = 'randy';
    // 第一个元素
    console.log(str.at(0)); // r
    // 倒数第一个元素
    console.log(str.at(-1)); // y
    // 倒数第二个元素
    console.log(str.at(-2)); // d

    const love='love'
    // 第一个元素
    console.log(love.at(0)); // l
    // 正数第二个元素
    console.log(love.at(1)); // o
    // 正数第三个元素
    console.log(love.at(2)); // v
    // 正数第四个元素
    console.log(love.at(3)); // e

ES14新特性

 ES14新增语法
    1.findLast() findLastIndex()
    const letters = [
      { value: 'z' },
      { value: 'y' },
      { value: 'x' },
      { value: 'y' },
      { value: 'z' },
    ];
    倒序查找 这两个函数都会从数组的末端开始寻找某个满足条件的元素。
    const found = letters.findLast((item) => item.value === 'y');
    const foundIndex = letters.findLastIndex((item) => item.value === 'y');
    console.log(found); // { value: 'y' }
    console.log(foundIndex); // 3

    2.toSorted()
    sort方法的复制版本,区别就是sort是修改原数组,而toSorted是返回新数组
    const arr = [1, 3, 5, 2, 8];
    const newArr = arr.sort();
    console.log("原数组:", arr); //[1, 2, 3, 5, 8
    console.log("新数组:", newArr);//[1, 2, 3, 5, 8]

    const arred = [1, 3, 5, 2, 8];
    const newArred = arred.toSorted();
    console.log("原数组:", arred);//[1, 3, 5, 2, 8]
    console.log("新数组:", newArred);//[1, 2, 3, 5, 8]

    3.toReversed()
    reverse方法的复制版本,区别就是reverse是修改原数组,而toReversed是返回新数组
    const arrver = [1, 3, 5, 2, 8];
    const newArrver = arrver.reverse();
    console.log("原数组:", arrver);//[8, 2, 5, 3, 1]
    console.log("新数组:", newArrver);//[8, 2, 5, 3, 1]

    const arrverd = [1, 3, 5, 2, 8];
    const newArrd = arrverd.toReversed();
    console.log("原数组:", arrverd);//[1, 3, 5, 2, 8]
    console.log("新数组:", newArrd);//[8, 2, 5, 3, 1]

    4.toSpliced()
    toSpliced与splice区别就很大了。splice是截取原数组的数据,并返回截取出来的数据。toSpliced是对原数组的副本进行操作,然后返回被截取完后的新数组,并不会修改原数组。
    toSpliced并不会影响原数组。返回的是截取后的数组
    const arrSpliced = [1, 3, 5, 2, 8];
    const newArrSpliced = arrSpliced.splice(1, 2);
    console.log("原数组:", arrSpliced);//[1, 2, 8]
    console.log("新数组:", newArrSpliced);//[3, 5]

    const arrEs = [1, 3, 5, 2, 8];
    const newArrEs = arrEs.toSpliced(1, 2);
    console.log("原数组:", arrEs);//[1, 3, 5, 2, 8]
    console.log("新数组:", newArrEs);//[1, 2, 8]

    5.with有点类似我们通过[index]来修改数组,区别就是with不是修改原数组,而是返回整个新数组
    const arrwith = [1, 3, 5, 2, 8];
    arrwith[1] = 10;
    console.log("原数组:", arrwith);//[1, 10, 5, 2, 8]

    const arrEsWith = [1, 3, 5, 2, 8];
    const newArrWith = arrEsWith.with(1, 10);
    console.log("原数组:", arrEsWith);//[1, 3, 5, 2, 8]
    console.log("新数组:", newArrWith);//[1, 10, 5, 2, 8]

后续有新增会加进去的 持续关注哈

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ES6-ES12是JavaScript的不同版本,每个版本都引入了新的特性和改进。以下是一些ES6到ES12的新特性的示例: ES6(2015年): 1. 箭头函数:简化了函数的语法。 2. 模板字面量:使用反引号(`)来创建多行字符串和插入变量。 3. 解构赋值:从对象或数组中提取值并赋给变量。 4. let和const:引入块级作用域的变量声明方式。 5. Promise:处理异步操作的更强大的方式。 ES7(2016年): 1. Array.prototype.includes():判断数组是否包含某个元素。 2. 指数操作符:使用双星号(**)进行指数运算。 ES8(2017年): 1. async/await:更简洁地处理异步操作。 2. Object.values()和Object.entries():从对象中提取值或键值对。 ES9(2018年): 1. Rest/Spread属性:通过...语法来处理函数参数和对象字面量。 2. Promise.prototype.finally():在Promise结束时执行操作。 ES10(2019年): 1. Array.prototype.flat()和Array.prototype.flatMap():用于处理嵌套数组的方法。 2. Object.fromEntries():将键值对列表转换为对象。 ES11(2020年): 1. 可选链操作符(Optional chaining):简化了访问深层嵌套属性的方式。 2. Nullish coalescing操作符:提供了一种默认值的设定方式。 ES12(2021年): 1. Promise.any():在多个Promise中返回最快解决的结果。 2. Logical Assignment Operators:提供了逻辑运算符与赋值的结合方式。 当然,以上仅是一些主要的新特性ES6-ES12还有其他许多有用的功能和语法改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值