|
2 | 2 |
|
3 | 3 | > Click :star:if you like the project. Pull Request are highly appreciated. Follow me [@SudheerJonna](https://twitter.com/SudheerJonna) for technical updates.
|
4 | 4 |
|
5 |
| -## Downloading PDF/Epub formats |
6 |
| - |
7 |
| -You can download the PDF and Epub version of this repository from the latest run on the [actions tab](https://github.com/sudheerj/ECMAScript-cheatsheet/actions). |
8 |
| - |
9 | 5 | ## How to run examples
|
10 | 6 | ```cmd
|
11 | 7 | npm install
|
@@ -35,6 +31,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
|
35 | 31 | | ES2019 Or ES10 | June 2019 |
|
36 | 32 | | ES2020 Or ES11 | June 2020 |
|
37 | 33 | | ES2021 Or ES12 | June 2021 |
|
| 34 | +| ES2022 Or ES13 | June 2022 | |
38 | 35 |
|
39 | 36 | ### Table of Contents
|
40 | 37 |
|
@@ -106,6 +103,13 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
|
106 | 103 | |3 | [WeakRef](#weakref)|
|
107 | 104 | |4 | [Numeric Separators](#numeric-separators)|
|
108 | 105 | |5 | [Logical Operators](#logical-operators)|
|
| 106 | +| | **ES2022 Or ES13**| |
| 107 | +|1 | [Top level await](#top-level-await) | |
| 108 | +|2 | [Class fields](#class-fields) | |
| 109 | +|3 | [Array at()](#array-at())| |
| 110 | +|4 | [Error cause](#error-cause)| |
| 111 | +|5 | [hasOwn](#has-own)| |
| 112 | +|6 | [Regex match indices](#regex-match-indices)| |
109 | 113 |
|
110 | 114 | ## ES2015 Or ES6
|
111 | 115 |
|
@@ -2173,3 +2177,257 @@ Most of these features already supported by some browsers and try out with babel
|
2173 | 2177 | ```
|
2174 | 2178 |
|
2175 | 2179 | **[⬆ Back to Top](#table-of-contents)**
|
| 2180 | +
|
| 2181 | +## ES2022 Or ES13 |
| 2182 | + ECMAScript 2022 or ES13 has been released in the month of June 2022 with some of important features in JavaScript. |
| 2183 | +
|
| 2184 | +1. ### Top-level await |
| 2185 | +
|
| 2186 | + In ES2022, it is possible to use `await` outside of the asynchronous (async) function scope, which makes it easier to use at the module level. This feaure delays the execution of current and parent modules until the imported module is loaded. |
| 2187 | +
|
| 2188 | + Let's take an example of `await` usage prior to ES2022, |
| 2189 | +
|
| 2190 | + ```javascript |
| 2191 | + import posts from './posts'; |
| 2192 | + |
| 2193 | + const getPosts = async() => { |
| 2194 | + let posts = await posts(); |
| 2195 | + return posts; |
| 2196 | + } |
| 2197 | + ``` |
| 2198 | +
|
| 2199 | + The usage of `await` is straightword with ES2022 as below, |
| 2200 | +
|
| 2201 | + ```javascript |
| 2202 | + let posts = await posts(); |
| 2203 | + ``` |
| 2204 | +
|
| 2205 | + There are few use cases to know the benefits of this top-level await. |
| 2206 | +
|
| 2207 | + #### Usecases |
| 2208 | +
|
| 2209 | + 1. **Dynamic dependency pathing:** |
| 2210 | + When you have a dynamic path for a dependency that depends on a runtime value, then await is helpful to load or import the messages at runtime. |
| 2211 | + ```javascript |
| 2212 | + const messages = await import(`./messages-${language}.js`); |
| 2213 | + ``` |
| 2214 | + 2. **Dependency fallbacks:** |
| 2215 | + If the imported module is failed to load, then fallback module loaded used to load the dependency. |
| 2216 | + ```javascript |
| 2217 | + let lodash; |
| 2218 | + try { |
| 2219 | + lodash = await import('https://first.domain.com/lodash'); |
| 2220 | + } catch { |
| 2221 | + lodash = await import('https://second.domain.com/lodash'); |
| 2222 | + } |
| 2223 | + ``` |
| 2224 | +
|
| 2225 | + 3. **Resource initialization:** |
| 2226 | + This feature can be used to initialize an app with Database. |
| 2227 | +
|
| 2228 | + ```javascript |
| 2229 | + import { dbConnector} from './dbUtils.js' |
| 2230 | + //connect to database |
| 2231 | + const connection = await dbConnector.connect(); |
| 2232 | + export default function(){ |
| 2233 | + connection.list() |
| 2234 | + } |
| 2235 | + ``` |
| 2236 | +
|
| 2237 | + **[⬆ Back to Top](#table-of-contents)** |
| 2238 | +
|
| 2239 | +2. ### Class fields |
| 2240 | +
|
| 2241 | + ES2015 introduced classes to javascript and class properties are defined in the constructor. In the below example, the Employee class has two fields defined in the constructor. |
| 2242 | +
|
| 2243 | + ```javascript |
| 2244 | + class Employee { |
| 2245 | + |
| 2246 | + constructor() { |
| 2247 | + this.name = "John"; //public |
| 2248 | + this._age=35; //private |
| 2249 | + } |
| 2250 | + |
| 2251 | + const employee = new Employee(); |
| 2252 | + employee.name = "Jack"; |
| 2253 | + employee._age =35; //No error |
| 2254 | + ``` |
| 2255 | +
|
| 2256 | + In the above example, the private `_age` property can be accessed and modified outside of the class. The prefix `_` is just a naming convention for private field and won't enforce any strict rule. |
| 2257 | +
|
| 2258 | + ES2022 introduced private and static fields to the classes. |
| 2259 | +
|
| 2260 | + 1. **Public and private fields or methods:** |
| 2261 | + ES2022 introduced public and private properties or methods like other programming languages. These properties and methods can be defined outside the constructor and private fields prefixed with # symbol followed by variable name. |
| 2262 | +
|
| 2263 | + In the below example, the Employee class has been defined with private variable outside the constructor. |
| 2264 | +
|
| 2265 | + ```javascript |
| 2266 | + class Employee { |
| 2267 | + name = "John"; |
| 2268 | + #age=35; |
| 2269 | + constructor() { |
| 2270 | + } |
| 2271 | + |
| 2272 | + #getAge() { |
| 2273 | + return #age |
| 2274 | + } |
| 2275 | + |
| 2276 | + } |
| 2277 | + |
| 2278 | + const employee = new Employee(); |
| 2279 | + employee.name = "Jack"; |
| 2280 | + employee.#age = 35; // Throws an error |
| 2281 | + ``` |
| 2282 | +
|
| 2283 | + 2. **Static fields and methods:** |
| 2284 | +
|
| 2285 | + Static fields and methods are applied to the class level and they can be accessed without an instance of a class. These fields and methods declared with `static` keyword |
| 2286 | +
|
| 2287 | + Let's define a employee class with static field and methods declated with `static` keyword. |
| 2288 | +
|
| 2289 | + ```javascript |
| 2290 | + class Employee{ |
| 2291 | + name = "John"; |
| 2292 | + static #employerName="Github" |
| 2293 | + |
| 2294 | + static #getEmployerName() { |
| 2295 | + return #employerName |
| 2296 | + } |
| 2297 | + } |
| 2298 | + const employee = new Employee(); |
| 2299 | + employee.emp = "Jack"; |
| 2300 | + employee.#employerName = 35; // Throws an error |
| 2301 | + |
| 2302 | + ``` |
| 2303 | +**[⬆ Back to Top](#table-of-contents)** |
| 2304 | +
|
| 2305 | +3. ### Array .at() method |
| 2306 | + The `.at()` method is used to access an array or string elements by passing the negative index value. i.e, It allows accessing values from the end of an array or from a string. |
| 2307 | +
|
| 2308 | + Before ES2022, You should have to access the elements from the end as below, |
| 2309 | +
|
| 2310 | + ```javascript |
| 2311 | + const array = [1, 2, 3, 4, 5]; |
| 2312 | + console.log(array[array.length - 2]); //4 |
| 2313 | + console.log(array.slice(-2)[0]); //4 |
| 2314 | + |
| 2315 | + const string = '12345'; |
| 2316 | + console.log(string[string.length - 2]); // '4' |
| 2317 | + console.log(string.slice(-2)[0]); // '4' |
| 2318 | + ``` |
| 2319 | +
|
| 2320 | + Now you should be able to write: |
| 2321 | +
|
| 2322 | + ```javascript |
| 2323 | + const array = [1, 2, 3, 4, 5]; |
| 2324 | + console.log(array.at(-2)); // 4 |
| 2325 | + |
| 2326 | + const string = '12345'; |
| 2327 | + console.log(string.at(-2)); |
| 2328 | + ``` |
| 2329 | +
|
| 2330 | + **[⬆ Back to Top](#table-of-contents)** |
| 2331 | +
|
| 2332 | +4. ### Error Cause |
| 2333 | +
|
| 2334 | + The `cause` property is added to the Error() constructor as an extra parameter which allow errors to be chained similar to Java-like stack traces in error chains. |
| 2335 | +
|
| 2336 | + In the below example, let's catch an error from JSON processing and re-throw it with a new meaningful message along with the original cause of the error. |
| 2337 | +
|
| 2338 | + ```javascript |
| 2339 | + function processUserData(arrayData) { |
| 2340 | + return arrayData.map(data => { |
| 2341 | + try { |
| 2342 | + const json = JSON.parse(data); |
| 2343 | + return json; |
| 2344 | + } catch (err) { |
| 2345 | + throw new Error( |
| 2346 | + `Data processing failed`, |
| 2347 | + {cause: err} |
| 2348 | + ); |
| 2349 | + } |
| 2350 | + }); |
| 2351 | + } |
| 2352 | + ``` |
| 2353 | +
|
| 2354 | + **[⬆ Back to Top](#table-of-contents)** |
| 2355 | +
|
| 2356 | +5. ### hasOwn |
| 2357 | + |
| 2358 | + The new `Object.hasOwn()` method is a replacement or improved version of `Object.prototype.hasOwnProperty`. It is a static method that returns true if the specified object has the indicated property as its own property. If the property is inherited, or does not exist, the method returns false. |
| 2359 | + |
| 2360 | + It's not only more concise and readable but also overcomes the below limitations of `hasOwnProperty`. |
| 2361 | +
|
| 2362 | + 1. **When `hasOwnProperty`overwritten:** |
| 2363 | +
|
| 2364 | + There are cases where you need to define customized `hasOwnProperty` on the object. When you try to apply `hasOwnProperty` to determine the own property or not, it throws an error as shown in below example. |
| 2365 | +
|
| 2366 | + ```javascript |
| 2367 | + const user = { |
| 2368 | + age: 35, |
| 2369 | + hasOwnProperty: ()=> { |
| 2370 | + return false; |
| 2371 | + } |
| 2372 | + }; |
| 2373 | + |
| 2374 | + user.hasOwnProperty('age') // throws a TypeError |
| 2375 | + ``` |
| 2376 | +
|
| 2377 | + This issue can be solved by hasOwn method . |
| 2378 | +
|
| 2379 | + ```javascript |
| 2380 | + const user = { |
| 2381 | + age: 35, |
| 2382 | + hasOwnProperty: ()=> { |
| 2383 | + return false; |
| 2384 | + } |
| 2385 | + }; |
| 2386 | + |
| 2387 | + user.hasOwn('age') // true |
| 2388 | + ``` |
| 2389 | +
|
| 2390 | + 2. **Create an object with create(null) function:** |
| 2391 | +
|
| 2392 | + If you create new object with help of create(null) function, then newly created object doesn’t inherit from Object.prototype. So it doesn't have hasOwnProperty method. |
| 2393 | + |
| 2394 | + Let's take an example to verify hasOwnProperty on an object created with create(null) function. |
| 2395 | + ```javascript |
| 2396 | + const user = Object.create(null); |
| 2397 | + user.age = 35; |
| 2398 | + user.hasOwnProperty('age'); // throws a TypeError |
| 2399 | + ``` |
| 2400 | +
|
| 2401 | + In this case, hasOwn() method can be used to determine the own property. |
| 2402 | +
|
| 2403 | + ```javascript |
| 2404 | + const user = Object.create(null); |
| 2405 | + user.age = 35; |
| 2406 | + user.hasOwn('age'); // true |
| 2407 | + ``` |
| 2408 | +
|
| 2409 | + **[⬆ Back to Top](#table-of-contents)** |
| 2410 | +
|
| 2411 | +6. ### Regex match indices |
| 2412 | +
|
| 2413 | + Regex match has been upgraded to include more information about the matching groups. The additional information includes starting and ending indices of matches in a RegExp with the usage of `\d` flag in the input string. |
| 2414 | +
|
| 2415 | + Let's take an example of regex pattern on the input string without `\d` flag and the information of the matches. |
| 2416 | +
|
| 2417 | + ```javascript |
| 2418 | + const regexPatter = /Jack/g; |
| 2419 | + const input = 'Authos: Jack, Alexander and Jacky'; |
| 2420 | + const result = [...input.matchAll(regexPatter)]; |
| 2421 | + console.log(result[0]); // ['Jack', index: 8, input: 'Authos: Jack, Alex and Jacky', groups: undefined] |
| 2422 | + ``` |
| 2423 | +
|
| 2424 | + Whereas `\d` flag provides an additional array with the indices of the different elements that match the regex, |
| 2425 | +
|
| 2426 | + ```javascript |
| 2427 | + const regexPatter = /(Jack)/gd; |
| 2428 | + const input = 'Authos: Jack, Alexander and Jacky'; |
| 2429 | + const result = [...input.matchAll(regexPatter)]; |
| 2430 | + console.log(result[0]); // ['Jack', 'Jack', index: 8, input: 'Authos: Jack, Alexander and Jacky', groups: undefined, indices: Array(2)] |
| 2431 | + ``` |
| 2432 | +
|
| 2433 | + **[⬆ Back to Top](#table-of-contents)** |
0 commit comments