ECMAScript 2022
JavaScript-Versionen
Alte ECMAScript-Versionen werden numerisch benannt: ES5 und ES6.
Ab 2016 werden Versionen nach dem Jahr benannt: ES2016, 2018, 2020, 2022.
Neue Funktionen von ES2022
- Array at()
- String at()
- RegExp /d
- Object.hasOwn()
- error.cause
- await import
- Klassische Felddeklarationen
- Private Methoden und Felder
Warnung:
Diese Funktionen sind relativ neu.
In alten Browsern möglicherweise Alternativcode (Polyfill) erforderlich.
JavaScript Array at()
ES2022 hat Array-Methoden eingeführt at()
:
Beispiel 1
Erhalten Sie das dritte Element des Arrays 'fruits':
const fruits = ['Banana', 'Orange', 'Apple', 'Mango']; let fruit = fruits.at(2);
Beispiel 2
Erhalten Sie das dritte Element des Arrays 'fruits':
const fruits = ['Banana', 'Orange', 'Apple', 'Mango']; let fruit = fruits[2];
at()
Diese Methode gibt das Element an der angegebenen Indexposition aus dem Array zurück.
at()
Method is equivalent to []
The returned results 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 |
Hinweis:
Viele Sprachen erlauben den Gebrauch von negativen Indizes (wie [-1]
) den letzten Element des Objekts/Arrays/Strings erreichen.
Dies ist in JavaScript nicht möglich, weil []
Sie wird verwendet, um Arrays und Objekte zu erreichen. obj[-1] bezieht sich auf den Wert des Schlüssels -1, nicht auf das letzte Attribut des Objekts.
at()
Diese Methode wurde in ES2022 eingeführt, um dieses Problem zu lösen.
JavaScript String at()
ES2022 hat Methoden für Strings eingeführt at()
:
Beispiel 1
Erhalten Sie das dritte Zeichen des Strings 'name':
const name = "W3Schools"; let letter = name.at(2);
Beispiel 2
Erhalten Sie das dritte Zeichen des Strings 'name':
const name = "W3Schools"; let letter = name[2];
at()
Method returns the character at the specified index from the string.
at()
Method is equivalent to []
The returned results 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 adds /d
Modifiers used to indicate the start and end of the match.
Instance
let text = "aaaabb"; let result = text.match(/(aa)(bb)/d);
RegExp modifiers are used to specify case-insensitive matching and other global searches:
Modifier | Description | Try it |
---|---|---|
g | Execute global matching (search all). | Try it |
i | Execute case-insensitive matching. | Try it |
d | Execute substring matching (added in ES2022). | Try it |
m | Execute multi-line matching. | Try it |
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.hasOwnProperty
but supports all object types.
Instance
Object.hasOwn(myObject, age)
Error Cause
ES2022 allows specifying the root cause of an error through error.cause.
Instance
try { connectData(); } throw new Error("Connecting failed.", { cause: err }); }
JavaScript await import
JavaScript modules can now wait for required resources before execution:
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 | Januar 2020 | September 2019 | April 2021 | Januar 2020 |
JavaScript-private Methoden und Felder
class Hello { #counter = 0; // Private Variable #myMethod() {} // Private Methode } const myClass = new Hello(); let x = myClass.#counter; // Fehler myClass.#myMethod(); // Fehler
Seit Juni 2021 werden private Methoden und Felder in allen modernen Browsern unterstützt:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 74 | Edge 79 | Firefox 90 | Safari 14.1 | Opera 62 |
April 2019 | Januar 2020 | Juni 2021 | April 2021 | Juni 2019 |