ES2022經Ecma大會批准

banq發表於2022-06-23

以下是ES2022新功能列表:

.at() 內建可索引的方法
.at內建可索引的方法。

const cart = ['1', '2', '3'];

// first element
cart.at(0); // '1'

// last element
cart.at(-1); // '2'

// out of bounds
cart.at(-100); // undefined 
cart.at(100); // undefined 
const int8 = new Int8Array([0, 10, 42, -10]);

// first element 
int8.at(0); // 0

// last element
int8.at(-1); // -10

// out of bounds
int8.at(-100) // undefined 
int8.at(100) // undefined
const sentence = 'This is a sample sentence'

// first element 
sentence.at(0); // 'T'

// last element
sentence.at(-1); // 'e'

// out of bounds
sentence.at(-100) // undefined
sentence.at(100) // undefined


正規表示式匹配索引
有關捕獲的子字串的開始和結束索引的附加資訊

/(?<xs>x+)(?<ys>y+)/.exec('xxxyyxx');
/*[
  'xxxyy',
  'xxx',
  'yy',
  index: 0,
  input: 'xxxyyxx',
  groups: [Object: null prototype] { xs: 'xxx', ys: 'yy' }
]*/
let input = "abcd";
let match = /b(c)/.exec(input);
let indices = match.indices;

// `indices` has the same length as match
indices.length === match.length

// The first element of `indices` contains the start/end indices of the match
indices[0]; // [1, 3];
input.slice(indices[0][0], indices[0][1]); // same as match[0]

// The second element of `indices` contains the start/end indices of the first capture
indices[1]; // [2, 3];
input.slice(indices[1][0], indices[1][1]); // same as match[1]);
Object.hasOwn
Object.hasOwn 

let books = {}
books.prop = 'exists';

// `hasOwn` will only return true for direct properties:
Object.hasOwn(books, 'prop');             // returns true
Object.hasOwn(books, 'toString');         // returns false
Object.hasOwn(books, 'hasOwnProperty');   // returns false

// The `in` operator will return true for direct or inherited properties:
'prop' in books;                          // returns true
'toString' in books;                      // returns true
'hasOwnProperty' in books;                // returns true


錯誤的原因
Cause 屬性指示錯誤的原因。

const actual = new Error('a better error!', { cause: 'Error cause' });

actual instanceof Error; // true
actual.cause; // 'Error cause'
try {
  maybeWorks();
} catch (err) {
  throw new Error('maybeWorks failed!', { cause: err });
}


頂級await
在模組中的非同步函式之外等待

// say this is index.mjs

// fails
await Promise.resolve('1');
// → SyntaxError: await is only valid in async function

// fix with wrapping
(async function() {
  await Promise.resolve('1');
  // → 2
}());

// to top-level await
await Promise.resolve('1') // '1'
const i18n = await import(`./content-${language}.mjs`);



類欄位宣告
公共和私有欄位的正交資訊組合。

class SampleClass {
    /*
      instead of:
      constructor() { this.publicID = 42; }
    */
    publicID = 42; // public field

    /*
      instead of:
      static get staticPublicField() { return -1 }
    */
    static staticPublicField = -1;

    // static private field
    static #staticPrivateField = 'private';

    //private methods
    privateMethod() {}

    // static block
    static {
      // executed when the class is created
    }
}


私有欄位的人體工程學brand檢查
沒有exceptions的brand檢查:

class C {
  井號brand;

  井號method() {}

  get 井號getter() {}

  static isC(obj) {
    // in keyword to check
    return 井號brand in obj && 井號method in obj && 井號getter in obj;
  }
}

 

相關文章