经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
ECMAScript13 中11个令人惊叹的 JavaScript 新特性
来源:cnblogs  作者:葡萄城技术团队  时间:2023/9/13 15:39:58  对本文有异议

前言

与许多其他编程语言一样,JavaScript 也在不断发展。每年,该语言都会通过新功能变得更加强大,使开发人员能够编写更具表现力和简洁的代码。 小编今天就为大家介绍ES13中添加的最新功能,并查看其用法示例以更好地理解它们。

1.类

在ES13之前,类字段只能在构造函数中声明。与许多其他语言不同,无法在类的最外层作用域中声明或定义它们。

  1. class Car {
  2. constructor() {
  3. this.color = 'blue';
  4. this.age = 2;
  5. }
  6. }
  7. const car = new Car();
  8. console.log(car.color); // blue
  9. console.log(car.age); //

而ES13 消除了这个限制。现在我们可以编写这样的代码:

  1. class Car {
  2. color = 'blue';
  3. age = 2;
  4. }const car = new Car();
  5. console.log(car.color); // blue
  6. console.log(car.age); // 2

2.私有方法和字段

ES13以前,不可能在类中声明私有成员。成员传统上带有下划线 ( _) 前缀,以表明它是私有的,但仍然可以从类外部访问和修改它。

  1. class Person {
  2. _firstName = 'Joseph';
  3. _lastName = 'Stevens'; get name() {
  4. return `${this._firstName} ${this._lastName}`;
  5. }
  6. }const person = new Person();
  7. console.log(person.name); // Joseph Stevens
  8. // 仍可以从类外部访问 // 原本打算设为私有的成员
  9. console.log(person._firstName); // Joseph
  10. console.log(person._lastName); // Stevens
  11. // 也可以修改
  12. person._firstName = 'Robert';
  13. person._lastName = 'Becker';console.log(person.name); // Robert Becker

使用 ES13,我们现在可以通过在类前面添加 ( #) 来向类添加私有字段和成员。尝试从外部访问这些类将会引发错误:

  1. class Person {
  2. #firstName = 'Joseph';
  3. #lastName = 'Stevens'; get name() {
  4. return `${this.#firstName} ${this.#lastName}`;
  5. }
  6. }const person = new Person();
  7. console.log(person.name);
  8. // 语法错误:私有字段 '#firstName' 必须在一个外层类中声明
  9. console.log(person.#firstName);
  10. console.log(person.#lastName);

3.await顶层操作

在 JavaScript 中,await运算符用于暂停执行,直到 一个Promise被解决(执行或拒绝)。 以前只能在async中使用此运算符。不可以在全局作用域中直接使用await。

  1. function setTimeoutAsync(timeout) {
  2. return new Promise((resolve) => {
  3. setTimeout(() => {
  4. resolve();
  5. }, timeout);
  6. });
  7. }
  8. //语法错误:await 仅在异步函数中有效
  9. await setTimeoutAsync(3000);

有了 ES13,现在我们可以:

  1. function setTimeoutAsync(timeout) {
  2. return new Promise((resolve) => {
  3. setTimeout(() => {
  4. resolve();
  5. }, timeout);
  6. });
  7. }
  8. // 等待超时 - 没有错误抛出
  9. await setTimeoutAsync(3000);

4.静态类字段和静态私有方法

现在可以在 ES13 中为类声明静态字段和静态私有方法。静态方法可以使用关键字this访问类中的其他私有/公共静态成员,实例方法可以使用this.constructor访问他们。

  1. class Person {
  2. static #count = 0; static getCount() {
  3. return this.#count;
  4. } constructor() {
  5. this.constructor.#incrementCount();
  6. } static #incrementCount() {
  7. this.#count++;
  8. }
  9. }const person1 = new Person();
  10. const person2 = new Person();console.log(Person.getCount()); // 2

5.类静态块

ES13 引入了一项特性,允许开发者定义仅在创建类时执行一次的静态块。这一特性与其他面向对象编程语言(如 C# 和 Java)中的静态构造函数相似。

在一个类的主体中,你可以定义任意数量的静态 {} 初始化块。它们会按照声明的顺序与任何交错的静态字段初始值设定项一起执行。此外,你还可以通过块中的 super 关键字访问超类的静态属性。这为开发者提供了更多的灵活性和控制能力。

  1. class Vehicle {
  2. static defaultColor = 'blue';
  3. }class Car extends Vehicle {
  4. static colors = []; static {
  5. this.colors.push(super.defaultColor, 'red');
  6. } static {
  7. this.colors.push('green');
  8. }
  9. }console.log(Car.colors); // [ 'blue', 'red', 'green' ]

6.检查对象中的私有字段

开发者如今可以利用这一新功能,使用运算符in来方便地检查对象是否包含某个特定的私有字段。

  1. class Car {
  2. #color; hasColor() {
  3. return #color in this;
  4. }
  5. }const car = new Car();
  6. console.log(car.hasColor()); // true;

通过运算符in,可以准确区分不同类中具有相同名称的私有字段。

  1. class Car {
  2. #color; hasColor() {
  3. return #color in this;
  4. }
  5. }class House {
  6. #color; hasColor() {
  7. return #color in this;
  8. }
  9. }const car = new Car();
  10. const house = new House();console.log(car.hasColor()); // true;
  11. console.log(car.hasColor.call(house)); // false
  12. console.log(house.hasColor()); // true
  13. console.log(house.hasColor.call(car)); // false

7.at() 索引方法

在 JavaScript 中,我们通常使用方括号[]来访问数组的第 t 个元素。这个过程非常简单,但实际上我们只是访问了索引为 t-1 的数组属性而已。

  1. const arr = ['a', 'b', 'c', 'd'];
  2. console.log(arr[1]); // b

然而,当我们希望通过方括号来访问数组末尾的第 N 个元素时,我们需要使用索引 arr.length - N。

  1. const arr = ['a', 'b', 'c', 'd'];
  2. // 从末尾开始第一个元素
  3. console.log(arr[arr.length - 1]); // d
  4. // 倒数第二个元素 console.log
  5. console.log(arr[arr.length - 2]); // c

借助全新的at()方法,可以以更加精简和富有表现力的方式来实现这一目标。要访问数组末尾的第N个元素,只需将负值-N作为参数传递给at()方法即可。

  1. const arr = ['a', 'b', 'c', 'd'];
  2. // 从末尾开始第一个元素
  3. console.log(arr.at(-1)); // d
  4. // 倒数第二个元素 console.log
  5. console.log(arr.at(-2)); // c

除了数组之外,字符串和TypedArray对象现在也有at()方法。

  1. const str = 'Coding Beauty';
  2. console.log(str.at(-1)); // y
  3. console.log(str.at(-2)); // tconst typedArray = new Uint8Array([16, 32, 48, 64]);
  4. console.log(typedArray.at(-1)); // 64
  5. console.log(typedArray.at(-2)); // 48

8.正则表达式匹配索引

在ES13之前,我们只能获取字符串中正则表达式匹配的起始索引,

  1. const str = 'sun and moon';const regex = /and/;const matchObj = regex.exec(str);// [ 'and', index: 4, input: 'sun and moon', groups: undefined ]
  2. console.log(matchObj);

使用ES13之后,可以通过指定一个/d正则表达式标志来获取匹配开始和结束的两个索引。这一特性赋予了更多的灵活性和控制能力。

  1. const str = 'sun and moon';
  2. const regex = /and/d;
  3. const matchObj = regex.exec(str);
  4. /**
  5. [
  6. 'and',
  7. index: 4,
  8. input: 'sun and moon',
  9. groups: undefined,
  10. indices: [ [ 4, 7 ], groups: undefined ]
  11. ]
  12. */
  13. console.log(matchObj);

设置标志后d,返回的对象将具有indices包含起始索引和结束索引的属性。

9.Object.hasOwn()方法

在 JavaScript 中,我们可以使用Object.prototype.hasOwnProperty()方法来检查对象是否具有给定的属性。

  1. class Car {
  2. color = 'green';
  3. age = 2;
  4. }const car = new Car();console.log(car.hasOwnProperty('age')); // true
  5. console.log(car.hasOwnProperty('name')); // false

然而,这种方法存在一些问题。首先,Object.prototype.hasOwnProperty()方法并未受到保护,这意味着我们可以通过自定义的hasOwnProperty()方法来覆盖它,而这个自定义方法可能会具有与Object.prototype.hasOwnProperty()不同的行为。需要额外注意的是这一点。

  1. class Car {
  2. color = 'green';
  3. age = 2; // This method does not tell us whether an object of
  4. // this class has a given property.
  5. hasOwnProperty() {
  6. return false;
  7. }
  8. }const car = new Car();console.log(car.hasOwnProperty('age')); // false
  9. console.log(car.hasOwnProperty('name')); // false

另外一个问题是,如果我们使用了 null 原型(通过 Object.create(null) 创建的对象),那么试图调用该方法将会产生错误。

  1. const obj = Object.create(null);
  2. obj.color = 'green';
  3. obj.age = 2;
  4. // TypeError: obj.hasOwnProperty 不是函数
  5. console.log(obj.hasOwnProperty('color'));

为了克服这些问题,我们可以利用属性调用方法Object.prototype.hasOwnProperty.call()来解决。具体示例如下所示:

  1. const obj = Object.create(null);
  2. obj.color = 'green';
  3. obj.age = 2;
  4. obj.hasOwnProperty = () => false;console.log(Object.prototype.hasOwnProperty.call(obj, 'color')); // true
  5. console.log(Object.prototype.hasOwnProperty.call(obj, 'name')); // false

这种方式并不十分便利。为了避免重复,我们可以编写一个可重用的函数,这样可以使我们的代码更加简洁和高效:

  1. function objHasOwnProp(obj, propertyKey) {
  2. return Object.prototype.hasOwnProperty.call(obj, propertyKey);
  3. }const obj = Object.create(null);
  4. obj.color = 'green';
  5. obj.age = 2;
  6. obj.hasOwnProperty = () => false;console.log(objHasOwnProp(obj, 'color')); // true
  7. console.log(objHasOwnProp(obj, 'name')); // false

现在不需要在那样做了,我们还可以使用全新的内置方法Object.hasOwn()来处理这个问题。它与我们之前编写的可重用函数类似,接受对象和属性作为参数,并且返回一个布尔值,如果指定的属性是对象的直接属性,则返回true;否则返回false。

  1. const obj = Object.create(null);
  2. obj.color = 'green';
  3. obj.age = 2;
  4. obj.hasOwnProperty = () => false;console.log(Object.hasOwn(obj, 'color')); // true
  5. console.log(Object.hasOwn(obj, 'name')); // false

10.错误原因属性

现在,错误对象已经增加了一个cause属性,该属性用于指定导致错误抛出的原始错误。通过这种方式,我们可以为错误添加额外的上下文信息,从而更好地诊断意外的行为。要指定错误的原因,我们可以在作为构造函数的第二个参数传递给Error()的对象中设置属性来实现。这种方法能够提供更丰富的错误追踪和调试信息。

  1. function userAction() {
  2. try {
  3. apiCallThatCanThrow();
  4. } catch (err) {
  5. throw new Error('New error message', { cause: err });
  6. }
  7. }try {
  8. userAction();
  9. } catch (err) {
  10. console.log(err);
  11. console.log(`Cause by: ${err.cause}`);
  12. }

11.从数组最后查找

在 JavaScript 中,我们已经可以使用Array的find()方法来查找数组中满足指定测试条件的元素。类似地,我们也可以使用findIndex()方法来获取满足条件的元素的索引值。尽管find()和findIndex()都是从数组的第一个元素开始搜索,但在某些情况下,从最后一个元素开始搜索可能会更有效。

有些情况下,我们知道从数组的末尾进行查找可能会获得更好的性能表现。例如,在这里我们尝试查找数组中prop属性等于"value"的项目。这时候,可以通过使用reverse()方法将数组反转,然后使用find()和findIndex()方法来从末尾开始搜索。下面是具体的实现示例:

  1. const letters = [
  2. { value: 'v' },
  3. { value: 'w' },
  4. { value: 'x' },
  5. { value: 'y' },
  6. { value: 'z' },
  7. ];const found = letters.find((item) => item.value === 'y');
  8. const foundIndex = letters.findIndex((item) => item.value === 'y');console.log(found); // { value: 'y' }
  9. console.log(foundIndex); // 3

上面的代码可以获取正确结果,但由于目标对象更接近数组的尾部,如果我们使用findLast()和findLastIndex()方法来从数组的末尾进行搜索,很可能能够显著提升程序的执行效率。通过这种方式,我们可以更快地找到所需的元素或索引,从而优化代码性能。

  1. const letters = [
  2. { value: 'v' },
  3. { value: 'w' },
  4. { value: 'x' },
  5. { value: 'y' },
  6. { value: 'z' },
  7. ];const found = letters.findLast((item) => item.value === 'y');
  8. const foundIndex = letters.findLastIndex((item) => item.value === 'y');console.log(found); // { value: 'y' }
  9. console.log(foundIndex); // 3

在一些特定的使用场景中,我们可能需要从数组的末尾开始搜索来获取准确的元素。举个例子,假设我们要查找数字列表中的最后一个偶数,使用find()或findIndex()方法可能会导致错误的结果:

  1. const nums = [7, 14, 3, 8, 10, 9];
  2. // 给出 14,而不是 10
  3. const lastEven = nums.find((value) => value % 2 === 0);
  4. // 给出 1,而不是 4
  5. const lastEvenIndex = nums.findIndex((value) => value % 2 === 0);console.log(lastEven); // 14
  6. console.log(lastEvenIndex); // 1

如果我们在调用reverse()方法之前使用数组的slice()方法创建新的数组副本,就可以避免不必要地改变原始数组的顺序。然而,在处理大型数组时,这种方法可能会导致性能问题,因为需要复制整个数组。

此外,findIndex()方法在反转数组时仍然无法达到预期效果,因为元素的反转会导致它们在原始数组中的索引改变。为了获取元素的原始索引,我们需要进行额外的计算,这意味着需要编写更多的代码来处理这种情况。

  1. const nums = [7, 14, 3, 8, 10, 9];
  2. // 在调用reverse()之前使用展开语法复制整个数组
  3. // calling reverse()
  4. const reversed = [...nums].reverse();
  5. // 正确给出 10
  6. const lastEven = reversed.find((value) => value % 2 === 0);
  7. // 给出 1,而不是 4
  8. const reversedIndex = reversed.findIndex((value) => value % 2 === 0);
  9. // 需要重新计算得到原始索引
  10. const lastEvenIndex = reversed.length - 1 - reversedIndex;console.log(lastEven); // 10
  11. console.log(reversedIndex); // 1
  12. console.log(lastEvenIndex); // 4

使用findLast()和findLastIndex()方法在需要查找数组中最后一个符合条件的元素或索引时非常实用。它们能够准确地定位目标对象,并且从数组末尾开始搜索,提供了高效的解决方案。

  1. const nums = [7, 14, 3, 8, 10, 9];const lastEven = nums.findLast((num) => num % 2 === 0);
  2. const lastEvenIndex = nums.findLastIndex((num) => num % 2 === 0);console.log(lastEven); // 10
  3. console.log(lastEvenIndex); // 4

结论

ES13 为 JavaScript 带来了一系列令人振奋的新功能,我们已经有幸见识了它们的魅力。通过运用这些功能,开发人员的工作效率将得到极大提升,同时也能以更加简洁、明晰的方式书写出更加纯净、精炼的代码。这些新特性为我们带来了更大的灵活性和便利性,使得我们的开发过程更加高效、愉悦。

原文链接:https://medium.com/coding-beauty/es13-javascript-features-eed7ed2f1497

扩展链接:

高级SQL分析函数-如何用窗口函数进行排名计算

3D模型+BI分析,打造全新的交互式3D可视化大屏开发方案

React + Springboot + Quartz,从0实现Excel报表自动化

原文链接:https://www.cnblogs.com/powertoolsteam/p/17692870.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号