返回

ES10 的 20 个精选示例,带您领略新特性

前端

ES10 虽然没有像 ES6 那么多新特性,但 ES10 仍然有一些有用的特性。文本通过简单示例来介绍了 ES10 新出来的特性。通过这种方式,咱们就可以快速理解,而不需要看太多的官方解释。

  1. Array.at

    Array.at() 方法返回给定索引的元素,如果索引小于 0,则从数组末尾开始计算。

    const array = [1, 2, 3, 4, 5];
    
    console.log(array.at(2)); // 3
    console.log(array.at(-2)); // 4
    
  2. BigInt

    BigInt 是一个新的原始数据类型,可以表示比 Number 类型更大的整数。

    const bigInt = 9007199254740991n;
    
    console.log(bigInt); // 9007199254740991n
    
  3. Symbol

    Symbol 是一个新的数据类型,表示一个唯一的标识符。Symbol 值可以作为对象属性的键,以确保键的唯一性。

    const symbol = Symbol();
    
    const object = {
      [symbol]: 'Hello, world!'
    };
    
    console.log(object[symbol]); // Hello, world!
    
  4. RegExp

    RegExp 对象现在支持 Unicode 属性。

    const regex = /\p{Letter}/u;
    
    const string = 'Hello, world!';
    
    console.log(regex.test(string)); // true
    
  5. 函数

    函数现在支持尾调用优化。

    function factorial(n) {
      if (n === 0) {
        return 1;
      } else {
        return n * factorial(n - 1);
      }
    }
    
    console.log(factorial(5)); // 120
    
  6. JSON

    JSON.stringify() 方法现在支持 BigInt。

    const bigInt = 9007199254740991n;
    
    const json = JSON.stringify({
      bigInt: bigInt
    });
    
    console.log(json); // {"bigInt":"9007199254740991"}
    
  7. 类现在支持私有字段和私有方法。

    class Person {
      #name;
    
      constructor(name) {
        this.#name = name;
      }
    
      #greet() {
        console.log(`Hello, my name is ${this.#name}!`);
      }
    
      greet() {
        this.#greet();
      }
    }
    
    const person = new Person('John Doe');
    
    person.greet(); // Hello, my name is John Doe!
    
  8. 实例

    实例现在支持私有字段和私有方法。

    class Person {
      #name;
    
      constructor(name) {
        this.#name = name;
      }
    
      #greet() {
        console.log(`Hello, my name is ${this.#name}!`);
      }
    }
    
    const person = new Person('John Doe');
    
    person.#greet(); // TypeError: Private field #greet is not accessible on the instance
    
  9. 箭头函数

    箭头函数现在支持尾调用优化。

    const factorial = (n) => {
      if (n === 0) {
        return 1;
      } else {
        return n * factorial(n - 1);
      }
    };
    
    console.log(factorial(5)); // 120
    
  10. Promise

Promise 对象现在支持 finally() 方法。

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('Hello, world!');
  }, 1000);
});

promise
  .then((result) => {
    console.log(result); // Hello, world!
  })
  .finally(() => {
    console.log('Finally!');
  });
  1. Set

Set 对象现在支持 Symbol 值。

const set = new Set();

const symbol = Symbol();

set.add(symbol);

console.log(set.has(symbol)); // true
  1. Map

Map 对象现在支持 Symbol 值。

const map = new Map();

const symbol = Symbol();

map.set(symbol, 'Hello, world!');

console.log(map.get(symbol)); // Hello, world!
  1. WeakSet

WeakSet 对象现在支持 Symbol 值。

const weakSet = new WeakSet();

const symbol = Symbol();

weakSet.add(symbol);

console.log(weakSet.has(symbol)); // true
  1. WeakMap

WeakMap 对象现在支持 Symbol 值。

const weakMap = new WeakMap();

const symbol = Symbol();

weakMap.set(symbol, 'Hello, world!');

console.log(weakMap.get(symbol)); // Hello, world!
  1. Proxy

Proxy 对象现在支持 Symbol 值。

const proxy = new Proxy({}, {
  get(target, property) {
    if (typeof property === 'symbol') {
      return 'Hello, world!';
    } else {
      return target[property];
    }
  }
});

console.log(proxy.symbol); // Hello, world!
  1. Reflect

Reflect 对象现在支持 Symbol 值。

const object = {
  [Symbol('foo')]: 'Hello, world!'
};

console.log(Reflect.get(object, Symbol('foo'))); // Hello, world!
  1. Internationalization

Internationalization API 现在支持新的语言标签。

const locale = 'en-US';

const numberFormatter = new Intl.NumberFormat(locale);

const formattedNumber = numberFormatter.format(12345.6789);

console.log(formattedNumber); // 12,345.6789
  1. Temporal

Temporal API 现在支持新的时间单位。

const temporalDate = Temporal.now();

console.log(temporalDate.year); // 2023
console.log(temporalDate.month); // 1
console.log(temporalDate.day); // 1
  1. 数组缩小

数组缩小语法允许您将数组声明为具有特定最大长度和元素类型的常量或变量。

// 定义一个最多可容纳 10 个字符串的数组
const names: string[] = ['John', 'Mary', 'Bob'];

// 定义一个最多可容纳 100 个数字的数组
const numbers: number[] = [1, 2, 3, 4, 5];
  1. 对象缩小

对象缩小语法允许您将对象声明为具有特定属性和类型的常量或变量。

// 定义一个具有 name 和 age 属性的对象
const person: { name: string; age: number; } = {
  name: 'John',
  age: 30
};

// 定义一个具有 name、age 和 address 属性的对象
const person2: { name: string; age: number; address: string; } = {
  name: 'Mary',
  age: 25,
  address: '123 Main Street'
};