ES2022

6475 ワード

at method

const array = [1, 2, 3];
const oldLast = array[array.length - 1]; // 3
const newLast = array.at(-1) // 3

'1234'.at(-1) // 4

Top-level await

// old
const fetchData = async () => {
	const res = await axios.get('...');
}
fetchData();

// new
const res = await axios.get('...');

Class Fields

class Person {
  // old
  constructor() {
    this.name = 'flynn'
  }

  // new 
  name = 'flynn'
}

Private Fields

class Person {
  // old 
  _name = 'flynn'

  get name() {
    return this.name;
  }

  // new
  #name = 'flynn'

  get name() {
    return this.#name;
  }
}

const person = new Person();
person._name // flynn
person.#name // throw Error!

Static Field

// old
class Circle {}
Circle.PI = 3.14

// new
class Circle {
  static PI = 3.14
}