ecma international 批准 ECMAScript 2022:新增功能是什么

**1. ECMAScript 2022 New Features** **New Members of Classes** ```javascript class MyClass { instancePublicField = 1; static staticPublicField = 2; #instancePrivateField = 3; static #staticPrivateField = 4; #nonStaticPrivateMethod() {} get #nonStaticPrivateAccessor() {} set #nonStaticPrivateAccessor(value) {} static #staticPrivateMethod() {} static get #staticPrivateAccessor() {} static set #staticPrivateAccessor(value) {} static { // Static initialization block } } ``` **Creating Properties (Public Slots)** - **Instance Public Fields** - **Static Public Fields** **Private Slots** Private slots are new and can be created in the following ways: - **Private Fields** (instance and static) - **Private Methods and Accessors** (non-static and static) - **Static Initialization Blocks** --- **2. Private Slot Checks via Operators** Private slot checks are also called "private domain ergonomics checks." These expressions validate if a private slot exists: - `obj#privateSlot` - `#privateSlot in obj` **Example:** ```javascript class ClassWithPrivateSlot { #privateSlot = true; static hasPrivateSlot(obj) { return #privateSlot in obj; } } const obj1 = new ClassWithPrivateSlot(); assert.equal(ClassWithPrivateSlot.hasPrivateSlot(obj1), true); const obj2 = {}; assert.equal(ClassWithPrivateSlot.hasPrivateSlot(obj2), false); ``` **Note:** Only private slots declared within the scope of their declaration can be referenced. --- **3. Top-Level `await`** Now, `await` can be used at the top level of modules without needing to import async functions or methods. **Example:** ```javascript // my-module.mjs const response = await fetch('https://example.com'); const text = await response.text(); console.log(text); ``` --- **4. `error.cause`** Subclasses of `Error` allow specifying which error caused the current error. **Example:** ```javascript try { // Do something } catch (otherError) { throw new Error('Something went wrong', { cause: otherError }); } ``` --- **5. `.at()` Method for Indexed Values** Indexed values (e.g., arrays, strings) support the `.at()` method, allowing access to elements via indices (like `[]`), and supports negative indices (different from `[]`). **Examples:** ```javascript ['a', 'b', 'c'].at(0) // 'a' ['a', 'b', 'c'].at(-1) // 'c' ``` **Supported Types:** - `string` - `Array` --- **6. Regular Expressions with Indexes** Adding flags to regexes returns match objects that record the start and end indices of captured groups (line numbers A and B). **Example:** ```javascript const matchObj = /(a+)(b+)/d.exec('aaaabb'); assert.equal(matchObj[1], 'aaaa'); assert.deepEqual(matchObj.indices[1], [0, 4]); // (A) assert.equal(matchObj[2], 'bb'); assert.deepEqual(matchObj.indices[2], [4, 6]); // (B) ``` --- **7. `Object.hasOwn()`** `Object.hasOwn(obj, propKey)` safely checks if an object has a non-inherited property with the key `propKey`. **Example:** ```javascript const proto = { protoProp: 'protoProp' }; const obj = { __proto__: proto, objProp: 'objProp' }; assert.equal('protoProp' in obj, true); // (A) assert.equal(Object.hasOwn(obj, 'protoProp'), false); // (B) assert.equal(Object.hasOwn(proto, 'protoProp'), true); // (C) ``` **Note:** - `(A)` Checks inherited properties - `(B)` Checks own properties - `(C)` Checks own properties --- **Common Questions** 1. **What is the difference between JavaScript and ECMAScript?** JavaScript is a programming language implemented on various platforms (browser, Node, .js, Deno, etc.). ECMAScript is its standard, as defined in the ECMAScript language specification. 2. **Who designed ECMAScript?** ECMAScript was designed by the TC39 committee of the Ecma International standard organization. 3. **How to add features to ECMAScript?** New ECMAScript features must be proposed to TC39. The process includes: - **Stage 0** (for comments and feedback) - **Stage 4** (ready for inclusion) Features reach Stage 4 and are added to ECMAScript versions. Version functional sets are typically frozen in March each year. Features reaching Stage 4 are added to the next ECMAScript version in January. 4. **Why is ECMAScript version importance?** The importance of ECMAScript versions has decreased since the TC39 process was established. What matters now is the stage of the proposed feature. Once it reaches Stage 4, it can be safely used. However, you must still check the target engine's support. 5. **Where to find features added in a specific ECMAScript version?** Features in specific ECMAScript versions are documented in: - **"JavaScript for Impatient Programmers"** (lists new features per version) - **TC39 Repository** (tables with proposed features and their target versions) - **ECMA-262 Repository** (pages with version details)