」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > ES3新特性:現代JavaScript的新功能

ES3新特性:現代JavaScript的新功能

發佈於2025-05-02
瀏覽:456

Modern JavaScript Features: What’s New in ES3 1。

array findlast和findlastIndex

您是否曾经从头开始在数组中找到一个项目? ES2023引入了Findlast和FindlastIndex,这就是这样做的。

Findlast:此方法在符合指定条件的数组中找到了最后一个元素。

    ; const lasteven = numbers.findlast(num => num%2 === 0); // 4 //查找在大型数组中18岁的最后一个用户 const用户= [/ *带有10000个用户的数组 */]; user.findlast(user => user.age === 18);
FindLastIndex:此方法返回最后一个元素的索引。
  const numbers = [1, 2, 3, 4, 5];
  const lastEven = numbers.findLast(num => num % 2 === 0); // 4

  // Find last user who is 18 years old in large array
  const users = [/* array with 10000 users */]; 
  users.findLast(user => user.age === 18);
  • 这些方法非常适合您需要逆转搜索逻辑,使代码更清晰,更有效的情况。
2。
  const lastEvenIndex = numbers.findLastIndex(num => num % 2 === 0); // 3

如果您在JavaScript中编写命令行工具,您将感谢HashBangs的新支持。通过添加#!在文件的顶部,您可以直接指定解释器,使您的脚本可执行而无需外部命令。

#!/usr/bin/env节点 console.log(“你好,世界!”); 这是一个小但方便的功能,特别是对于那些在node.js中构建CLI工具的人。

3。

符号为feepMap键

#!/usr/bin/env node

console.log("Hello, world!");

这种增强使虚弱映射更加通用,尤其是当您需要符号提供的独特的无碰撞键时。

4。数组分组通过方法

通过新组方法进行分组数组元素变得容易得多。

组:此方法将您的数组组织成一个对象,并由您提供的函数确定的键和值为匹配每个密钥的元素数组。
  const numbers = [1, 2, 3, 4, 5];
  const lastEven = numbers.findLast(num => num % 2 === 0); // 4

  // Find last user who is 18 years old in large array
  const users = [/* array with 10000 users */]; 
  users.findLast(user => user.age === 18);

const Animal = [ {type:'哺乳动物',名称:'dog'}, {type:'bird',名称:'sparrow'}, {type:'哺乳动物',名称:'cat'}, ]; const grouped = Animal.group(({type})=> type); console.log(分组); // { //哺乳动物:[{type:'哺乳动物',name:'dog'},{type:'mammal',name:'cat'}], //伯德:[{type:'bird',名称:'sparrow'}]] //}

此功能非常适合您需要快速有效地对数据进行分类的方案。 5。 array.prototype.tosorted and array.prototype.toreversed()

排序阵列刚好有很多清洁。与变化原始数组的类别不同,TOSORTED返回一个新的排序阵列和toreversed返回一个新的反向数组,而原始的未触及。
  • ; const sortedarr = arr.toSorted(); console.log(sortedarr); // [1,1,3,4,5] console.log(arr); // [3,1,4,1,5] 令数据= [/ *不应修改的重要数据 */]; 令反向data = data.toreversed(); //安全反向
此方法非常适合当您在使用排序版本时需要保留原始数组时。
  const animals = [
    { type: 'mammal', name: 'dog' },
    { type: 'bird', name: 'sparrow' },
    { type: 'mammal', name: 'cat' },
  ];

  const grouped = animals.group(({ type }) => type);
  console.log(grouped);
  // {
  //   mammal: [{ type: 'mammal', name: 'dog' }, { type: 'mammal', name: 'cat' }],
  //   bird: [{ type: 'bird', name: 'sparrow' }]
  // }
array.prototype.with

const数= [10,20,30,40]; const newnumbers =数字。(2,25); // [10,20,25,40]

当您想不成熟时更新数组时,此方法是完美的,使以功能编程模式管理状态更容易。

7。

const arr = [3, 1, 4, 1, 5];
const sortedArr = arr.toSorted();
console.log(sortedArr); // [1, 1, 3, 4, 5]
console.log(arr); // [3, 1, 4, 1, 5]

let data = [/* important data that shouldn't be modified */];
let reversedData = data.toReversed(); // Safely reverse

这是处理异步操作的一种整洁而简洁的方法,尤其是当您需要从其构造函数外部控制承诺的结果时。

ES2023的浏览器支持

ES2023是JavaScript的最新版本,因为它刚刚在2023年完成。这意味着并非所有Web浏览器都可以使用其新功能,但是它们开始使用:


Safari尚未支持这些功能。

  const numbers = [1, 2, 3, 4, 5];
  const lastEven = numbers.findLast(num => num % 2 === 0); // 4

  // Find last user who is 18 years old in large array
  const users = [/* array with 10000 users */]; 
  users.findLast(user => user.age === 18);
node.js

array.prototype.findlast()

array.prototype.findlastindex() 预计将来会在将来的更新中添加更多ES2023功能。


const arr = [3, 1, 4, 1, 5];
const sortedArr = arr.toSorted();
console.log(sortedArr); // [1, 1, 3, 4, 5]
console.log(arr); // [3, 1, 4, 1, 5]

let data = [/* important data that shouldn't be modified */];
let reversedData = data.toReversed(); // Safely reverse

transpilers:

为了使用新的ES2023功能,开发人员可以将ES2023代码变成较旧的版本,使用称为Transpilers(例如Babel)的工具,更多的浏览器理解。这样,即使浏览器还没有准备好,您也可以开始使用新的东西。

现在,对ES2023的支持仍在增长。像Firefox和Chrome这样的大浏览器开始包括其一些功能。有关在哪里支持的内容的详细信息,您可以查看我可以使用。使用转板器有助于使这些新功能今天可用,而我们等待浏览器在未来几年中赶上。

结论

ES2023带来了一系列新功能,使JavaScript更强大,更易于使用。从增强的阵列方法到更好的承诺处理,这些更新都在于使您的代码清洁和更有效。随着JavaScript的不断发展和发展,保持最新的这些更改,确保您始终充分利用语言。
    参考:
  • tc39提案
  • 草稿ECMA-262

    mdn
版本聲明 本文轉載於:https://dev.to/manjushsh/modern-javascript-features-whats-new-in-es2023-4i7d?1如有侵犯,請聯繫[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3