Geek Culture
Published in

Geek Culture

Proposals included in the incoming ECMAScript 2022 standard for JavaScript

Photo by Fahrul Razi on Unsplash

Class field declarations

class Post{
title;
content;
shares = 0;
}

Private methods and fields

class User {
name;
#password;
}
class User {
name;
#password;
get #password(){
return #password;
}
#clear(){
this.name = null;
this.#password = null;
}
}

Static public methods and fields

class Environment {
name;
port;
static hostname = 'localhost';
static get hostname(){
return hostname;
}
}

Regexp Match Indices

const regexp = /test(\d)/g; //without the /d flag
const regexp2022 = /test(\d)/dg; //with the /d flag
const str = 'test1test2';const array = [...str.matchAll(regexp)];
const array2022 = [...str.matchAll(regexp2022)];
console.log(array[0]);
console.log(array2022[0]);
console.log(array[1]);
console.log(array2022[1]);

Top-level await

import {getUser} from './data/user'

let user = await getUser();
//

Ergonomic brand checks for Private Fields

class User {
name;
#password;
get #password(){
return #password;
}
#clear(){
this.name = null;
this.#password = null;
}
static hasPassword(obj){
return #password in obj;
}
}

New .at() method on all the built-in indexables

const array = [0,1,2,3,4,5];console.log(array[array.length-1]); // 5
console.log(array.at(-1)); //5
//same behaviour
console.log(array[array.lenght-2]); // 4
console.log(array.at(-2)); //4
//same behaviour

Accessible Object.prototype.hasOwnProperty()

const obj = { foo:'bar' }let hasFoo = Object.prototype.hasOwnProperty.call(obj, 'foo');console.log(hasFoo); //true
const obj = { foo:'bar' }let hasFoo = Object.hasOwn(obj, 'foo');console.log(hasFoo); //true

ECMAScript class static initialization blocks

Without static blocks:


class User {
static roles;
name;
#password;
}

try {
User.roles = getRolesFromDb();
} catch {
User.roles = getRolesFromBackup();
}
class User {
static roles;
name;
#password;
static {
try {
User.roles = getRolesFromDb();
} catch {
User.roles = getRolesFromBackup();
}
}
}
I will appreciate and remember it forever ❤

References and more info:

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store