ECMAScript 2022

Versienummer van JavaScript

Oude ECMAScript-versies worden genaamd naar nummers: ES5 en ES6.

Sinds 2016 worden versies genaamd naar het jaartal: ES2016, 2018, 2020, 2022.

Nieuwe functies van ES2022

Waarschuwing:

Deze functies zijn relatief nieuw.

Oude browsers kunnen alternatieve code (Polyfill) nodig hebben.

JavaScript Array at()

ES2022 heeft de arraymethode geïntroduceerd at():

Voorbeeld 1

Ophalen van het derde element van het fruits-array:

const fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
let fruit = fruits.at(2);

Try it yourself

Voorbeeld 2

Ophalen van het derde element van het fruits-array:

const fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
let fruit = fruits[2];

Try it yourself

at() De methode retourneert het element op de opgegeven index van het array.

at() Method is equivalent to [] The results returned are the same.

Starting from March 2022, all modern browsers support at() Method:

Chrome Edge Firefox Safari Opera
Chrome 92 Edge 92 Firefox 90 Safari 15.4 Opera 78
April 2021 July 2021 July 2021 March 2022 August 2021

Let op:

Vele talen staan negatieve indices toe (zoals [-1]) Het benaderen van het laatste element van een object/arrays/strings.

Dit is in JavaScript niet mogelijk omdat [] Gebruikt om arrays en objecten te benaderen. obj[-1] verwijst naar de waarde van de sleutel -1, niet naar de laatste eigenschap van het object.

at() De methode is in ES2022 geïntroduceerd om dit probleem op te lossen.

JavaScript String at()

ES2022 heeft de stringmethode geïntroduceerd at():

Voorbeeld 1

Ophalen van het derde letter van de string 'name':

const name = "W3Schools";
let letter = name.at(2);

Try it yourself

Voorbeeld 2

Ophalen van het derde letter van de string 'name':

const name = "W3Schools";
let letter = name[2];

Try it yourself

at() Method returns the character at the specified index from the string.

at() Method is equivalent to [] The results returned are the same.

Starting from March 2022, all modern browsers support at() Method:

Chrome Edge Firefox Safari Opera
Chrome 92 Edge 92 Firefox 90 Safari 15.4 Opera 78
April 2021 July 2021 July 2021 March 2022 August 2021

RegExp d modifier

ES2022 added /d Modifiers, used to indicate the start and end of the match.

Instance

let text = "aaaabb";
let result = text.match(/(aa)(bb)/d);

Try it yourself

RegExp modifiers are used to specify case-insensitive matching and other global searches:

Modifier Description Try it yourself
g Execute global matching (find all). Try it yourself
i Execute case-insensitive matching. Try it yourself
d Execute substring matching (added in ES2022). Try it yourself
m Execute multi-line matching. Try it yourself

Object.hasOwn()

ES2022 provides a safe method to check if a property is an own property of an object.

Object.hasOwn() Similar to Object.prototype.hasOwnPropertybut supports all object types.

Instance

Object.hasOwn(myObject, age)

Try it yourself

Error Cause

ES2022 allows specifying the root cause of an error through error.cause.

Instance

try { 
  connectData(); 
} 
  throw new Error("Connecting failed.", { cause: err }); 
}

Try it yourself

JavaScript await import

JavaScript modules can now wait for the resources to be imported before running:

import {myData} from './myData.js';
const data = await myData();

JavaScript class field declarations

class Hello {
  counter = 0; // class field
}
const myClass = new Hello();
let x = myClass.counter;

Starting from April 2021, class field declarations are supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Chrome 72 Edge 79 Firefox 69 Safari 14.1 Opera 60
January 2019 Januari 2020 September 2019 April 2021 Januari 2020

JavaScript Privaat Methods en Velden

class Hello {
  #counter = 0;  // Privaat Veld
  #myMethod() {} // Privaat Method
}
const myClass = new Hello();
let x = myClass.#counter; // Fout
myClass.#myMethod();      // Fout

Sinds juni 2021 worden privaat methods en velden ondersteund in alle moderne browsers:

Chrome Edge Firefox Safari Opera
Chrome 74 Edge 79 Firefox 90 Safari 14.1 Opera 62
April 2019 Januari 2020 Juni 2021 April 2021 Juni 2019