ES2022新语法

  • at()方法对数组索引
  • 正则表达式索引
  • Object.hasOwn方法
  • 抛出异常
  • 线程同步等待
  • 函数属性声明
  • 通过标签检查私有属性

.at() 方法对数组索引

const cart = ['苹果', '香蕉', '菠萝'];

// first element
cart.at(0); // '苹果'

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

// 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

抛出异常

通过属性来状态错误的原因

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 升级

await可以在async方法的外面使用。


// fails
await Promise.resolve('apple');
// → 会报错: await is only valid in async function

// 之前用法时这样
(async function() {
  await Promise.resolve('apple');
  // 
}());

// 升级后的 await 可以直接这样用了
await Promise.resolve('apple') // 'apple'
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
    }
}

通过标签检查私有属性

合理的通过标签检查私有属性。

class C {
  #brand;

  #method() {}

  get #getter() {}

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

猜你喜欢

转载自blog.csdn.net/playboyanta123/article/details/125554510