Questions tagged [es2022]

ECMAScript 2022 (ES2022) is the 13th version of the ECMAScript language. It adds new features including Array.at(), RegExp match indices, and error.cause. Only use this tag where the question specifically relates to new features or technical changes provided in ECMAScript 2022.

ECMAScript 2022 (ES2022) is the 13th version of the language.

A full list of finished proposals can be found here.

21 questions
2040
votes
63 answers

Get the last item in an array

Here is my JavaScript code so far: var linkElement = document.getElementById("BackButton"); var loc_array = document.location.href.split('/'); var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2])));…
balexander
  • 23,131
  • 14
  • 45
  • 68
560
votes
41 answers

Private properties in JavaScript ES6 classes

Is it possible to create private properties in ES6 classes? Here's an example. How can I prevent access to instance.property? class Something { constructor(){ this.property = "test"; } } var instance = new…
d13
  • 9,817
  • 12
  • 36
  • 44
129
votes
11 answers

What is property in hasOwnProperty in JavaScript?

Consider: if (someVar.hasOwnProperty('someProperty') ) { // Do something(); } else { // Do somethingElse(); } What is the right use/explanation of hasOwnProperty('someProperty')? Why can't we simply use someVar.someProperty to check if an object…
FLY
  • 2,379
  • 5
  • 29
  • 40
9
votes
5 answers

How to find last element of an array without modifying source array in Vanilla Javascript

I have an array contains const data = ['a', 'b', 'c', 'd']; how to find the last element, result should be 'd'
Syam Prasad
  • 149
  • 1
  • 1
  • 6
6
votes
1 answer

ClassDecorator with Angular (15) targeting ES2022

We currently are in march 2023 and I am looking for a way to make my class decorators work the right way, with no warning from the cli. Here my simple code : function MyDecorator(myConstructor: new (...args: any[]) => any): new (...args: any[]) =>…
M'sieur Toph'
  • 2,534
  • 1
  • 21
  • 34
3
votes
1 answer

Dynamic import assertion from loader module resolve step in nodejs v18

i have a custom --loader module, where i want to assign the import assertion type to json modules to maintain backwards compatibility: export async function resolve(specifier,context,defaultResolve) {let {importAssertions:assert}=context||{}; …
bpstrngr
  • 339
  • 3
  • 13
2
votes
1 answer

why class private property still can be accessed in chrome 114?

class Person { #name = 'Ergonomic brand checks for Private Fields'; aa=1 static check(obj) { return #name in obj; } } var p = new Person() p.#name can be accessed! means not supported... p.#age has error report, means supported...
flyflydogdog
  • 307
  • 3
  • 10
2
votes
1 answer

Typescript TypeError with es2022 but not with es2021

I'm trying to update the target property of the tsconfig.json file from es2015 to es2022. But I get an error when running the tests, which I think only use tsc without babel: Chrome Headless 110.0.5481.177 (Mac OS 10.15.7) TypeError: Cannot read…
GarfieldKlon
  • 11,170
  • 7
  • 31
  • 33
2
votes
1 answer

About importing modules in TypeScript

I have this file structure: ∟ src ∟ math ├ funcs.ts ├ constants.ts ├ index.ts ├ index.ts On my src/index.ts, I tried this: import * as math from './math/index.js'; console.log(`pi = ${math.PI}`); console.log(`The area of a circle…
K09P
  • 480
  • 4
  • 13
1
vote
1 answer

Does swapping two variables using the destructuring assignment not work with ES2022 method at() in JavaScript?

So I wanted to swap the first and the last items of an array with the destructuring assignment. I tried using the ES2022 method array.at() to get the values of indexes: const array = ['a, 'i', 'r']; [array.at(0), array.at(-1)] = [array.at(-1),…
Caesar
  • 41
  • 6
1
vote
1 answer

Why is typescript optional class property is set to undefined for es2022

Typescript compiler handling of optional class properties seems to have changed from es2021 to es2022. class A { a?: string b?: string constructor() { this.b = 'asd' } } console.log(new A()) with tsconfig target=es2021…
mrtnlrsn
  • 1,105
  • 11
  • 19
1
vote
1 answer

Web component not working for old Chrome versions after update to Angular 15

After updating Angular to version 15, where the target of compilerOptions is automatically set to ES2022, my angular web component is not working anymore for Chrome 53. This error is showing in console: Uncaught SyntaxError: Unexpected token { I am…
NiZa
  • 3,806
  • 1
  • 21
  • 29
1
vote
1 answer

Inherit execution of static initializer blocks in JavaScript

I have code like: class A { static { console.log("A"); } } class B extends A { static { console.log("B"); } } Why doesn't it print A twice? I would like a way in JavaScript to execute code of a class whenever it is…
kungfooman
  • 4,473
  • 1
  • 44
  • 33
1
vote
0 answers

Typescript ES2022 function call

Out of curiosity I checked the generated javascript with typescript target configured to ES2022 and I saw something like this: function leFormat(a) { console.log(`yes`, a) } (0, leFormat)(new Date()) Why typescript chooses to do this? Why not…
Lucas Steffen
  • 1,244
  • 2
  • 10
  • 22
1
vote
0 answers

How to catch the error of Private field must be declared in an enclosing class?

I have this sample code: class Person { #name = "Jack"; static hasTitle(obj) { try { console.log(obj.#age); } catch (error) { console.log(Object.keys(error)); } return #name in obj; } } const p = new…
Omar Dulaimi
  • 846
  • 10
  • 30
1
2