Skip to content

Latest commit

 

History

History
101 lines (71 loc) · 3.25 KB

ES2023.MD

File metadata and controls

101 lines (71 loc) · 3.25 KB

ES2023 "ES14" credit1 credit2

See the ES2023 standard for full specification of the ECMAScript 2023 language.

ES2023 includes the following new feature proposals:


Array find from last

Array.prototype.findLast and Array.prototype.findLastIndex 📕

let nums = [5,4,3,2,1];

let lastEven = nums.findLast((num) => num % 2 === 0); // 2

let lastEvenIndex = nums.findLastIndex((num) => num % 2 === 0); // 3

Hashbang Grammar

#! for JS 📕

The first line of this script begins with #!, which indicates a comment that can contain any text.

#!/usr/bin/env node
// in the Script Goal
'use strict';
console.log(1);

Symbols as WeakMap keys

Use symbols in weak collections and registries 📕

Note: registered symbols aren’t allowed as weakmap keys.

let sym = Symbol("foo");
let obj = {name: "bar"};
let wm = new WeakMap();
wm.set(sym, obj);
console.log(wm.get(sym)); // {name: "bar"}

sym = Symbol("foo");
let ws = new WeakSet();
ws.add(sym);
console.log(ws.has(sym)); // true

sym = Symbol("foo");
let wr = new WeakRef(sym);
console.log(wr.deref()); // Symbol(foo)

sym = Symbol("foo");
let cb = (value) => {
  console.log("Finalized:", value);
};
let fr = new FinalizationRegistry(cb);
obj = {name: "bar"};
fr.register(obj, "bar", sym);
fr.unregister(sym);

Change Array by copy

return changed Array and TypedArray copies 📕

Note: typed array doesn’t have tospliced.

const greek = ['gamma', 'alpha', 'beta']
greek.toSorted(); // [ 'alpha', 'beta', 'gamma' ]
greek; // [ 'gamma', 'alpha', 'beta' ]

const nums = [0, -1, 3, 2, 4]
nums.toSorted((n1, n2) => n1 - n2); // [-1,0,2,3,4]
nums; // [0, -1, 3, 2, 4]

const greek = ['gamma', 'alpha', 'beta']
greek.toReversed(); // [ 'beta', 'alpha', 'gamma' ]
greek; // [ 'gamma', 'alpha', 'beta' ]

const greek = ['gamma', 'alpha', 'beta']
greek.toSpliced(1,2); // [ 'gamma' ]
greek; // [ 'gamma', 'alpha', 'beta' ]

greek.toSpliced(1,2, ...['delta']); // [ 'gamma', 'delta' ]
greek; // [ 'gamma', 'alpha', 'beta' ]

const greek = ['gamma', 'alpha', 'beta']
greek.toSpliced(1,2); // [ 'gamma' ]
greek; // [ 'gamma', 'alpha', 'beta' ]

greek.toSpliced(1,2, ...['delta']); // [ 'gamma', 'delta' ]
greek; // [ 'gamma', 'alpha', 'beta' ]

const greek = ['gamma', 'alpha', 'beta'];
greek.with(2, 'bravo'); // [ 'gamma', 'alpha', 'bravo' ]
greek; //  ['gamma', 'alpha', 'beta'];