3

ES2022经Ecma大会批准

 1 year ago
source link: https://www.jdon.com/61210
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ES2022经Ecma大会批准
以下是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;
  }
}

 


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK