Some features stand out in javascript ES12
- Introduce
- Private Accessors
- Promise.any () and AggregateError
- Numeric Separators
- String.prototype.replaceAll ()
- Logical Assignment Operators
- Conclude
1. Introduction
- ES12 2021 is in phase 4 and is expected to launch this year with many useful features.
- It allows us to be easy to use, saves time, and makes the code a lot easier to read.
- These outstanding features will be listed in the article below.
2. Private Accessors
- Accessors functions can be made private by writing # before the function
For example:
1 2 3 4 5 6 7 8 9 10 | class Cat { // Public accessor get cat_name() { return "Luchi" } set cat_name(value) {} // Private accessor get #cat_age() { return 2 } set #cat_age(value) {} } |
- get and set in the above code is the keyword that makes cat_name () an access attribute. Although cat_name () looks like a function, it can be read as an ordinary property like so:
1 2 3 4 | const obj = new Cat(); console.log(obj.cat_name); // "Luchi" console.log(obj.cat_age); // undefined |
3. Promise.any () and AggregateError
- Promise.any () is called immediately after any promises are made or all of them are rejected.
1 2 3 4 5 6 7 8 9 10 11 12 13 | Promise.any([ fetch('https://dantri.com/').then(() => 'dantri'), fetch('https://facebook.com/').then(() => 'facebook'), fetch('https://volam2.vn/').then(() => 'volam2') ]).then((first) => { // Any of the promises was fulfilled. console.log(first); // -> 'dantri' }).catch((error) => { // All of the promises were rejected. console.log(error); }) |
- Three concurrent requests are shown in the code above. Once one of the requests is resolved, Promise.any is also resolved and records the first resolved request in the console (dantri in the example above).
- If all promises are rejected, Promise.any will throw a new exception of the error: AggregateError.
- What’s new about it is that the AggregateError object represents an error where several errors are encapsulated in a single error.
- It should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 | Promise.any([ Promise.reject(new Error("error 1")), Promise.reject(new Error("error 2")), Promise.reject(new Error("error 3")), ]).then((first) => { console.log(first); }).catch((error) => { console.log(e.message); // "All Promises rejected" console.log(e.name); // "AggregateError" console.log(e.errors); // [ Error: "error 1", Error: "error 2", Error: "error 3" ] }) |
- The above e.errors is an array of errors object.
4. Numeric Separators
- When we work with large numbers it can be difficult or confusing. For example: “21989854”. Look closely to see that this is 21 million and …
- With the new feature of ES2021, we can rewrite with underscores to make it easier to see as “21_989_854”. It was much easier to see by looking at it. Now, we can clearly see that’s 21 million.
- Hence, the Numeric Separators feature is meant to simply improve readability. It does not affect the performance of our application.
1 2 3 4 5 6 | // previous syntax before ES12 const money = 1248723742; // new syntax coming with ES12 const money = 1_248_723_742; |
- Looking at the example above we see that it is much easier to read.
5. String.prototype.replaceAll ()
- In ES12 replaceAll is simplified in usage than before, instead of using regex we have simpler syntax.
1 2 3 4 5 6 7 8 9 10 | // previous syntax before ES12 const fullname = 'fullname = Nguyen + Xuan + Hung'; const fullnameFormated = fullname.replace (/ + / g, ''); // Nguyen Xuan Hung // new syntax coming with ES12 const fullname = 'fullname = Nguyen + Xuan + Hung'; const fullnameFormated = fullname.replaceAll ('+', ''); // Nguyen Xuan Hung |
6. Logical Assignment Operators
- With ES12, there is a combination of the logic operator and the assignment expression:
1 2 3 4 5 6 7 | a || = b // Tương đương với: a || (a = b), chỉ gán nếu a là Falsy. a && = b // Tương đương với: a && (a = b), chỉ gán nếu a là Truthy. a ?? = b // Tương đương với: a ?? (a = b), chỉ gán nếu a là Nullish. |
7. Conclusion
- These are the new functions of ES12 I learned, you can refer to below:
- https://js.plainenglish.io/javascript-es2021-es12-new-features-7aa5f411d81f
- https://backbencher.dev/javascript/es2021-new-features
- https://www.pullrequest.com/blog/javascript-es2021-you-need-to-see-these-es12-features/
- https://medium.com/better-programming/the-top-3-new-javascript-es-2021-es-12-features-im-excited-about-a3ac129efbb2