ECMAScript 2021 includes:
ECMAScript 2021 was approved by ECMA International on 22 June 2021. ECMAScript is a standardized JavaScript language. The first version was released in 1997 and has grown to become one of the most widely used general-purpose programming languages in the world. This Ecma standard defines the ECMAScript 2021 Language and is the 12th edition of the ECMAScript Language specification.
String.prototype.replaceAll
Previously, if you wanted to replace all String occurrences, you needed to use a combination of String.prototype.replace and global regexp. Now, the String. The prototype. The replaceAll simplify it.
const str = "hello-world";
// before
str.replace(/-/g."_")
// "hello_world"
// now
str.replaceAll("-"."_")
// "hello_world"
Copy the code
To view more
Promise.any
Any has been added to the list of Promise Combinators in the 2021 specification. When you want to deal with the first fulfills Promise, use promise.any. Unlike promise.race, when one promises fail, it does not reject. See Promise Combinators Explained for more details.
// Check which website is faster
Promise.any([
fetch('https://v8.dev/').then(() = > 'home'),
fetch('https://v8.dev/blog').then(() = > 'blog'),
fetch('https://v8.dev/docs').then(() = > 'docs')
]).then((first) = > {
// Any of the promises was fulfilled.
console.log(first);
/ / to 'home'
}).catch((error) = > {
// All of the promises were rejected.
console.log(error);
});
Copy the code
To view more
WeakRefs
The WeakRefs proposal brings two new Contructors to the language: WeakRef and FinalizationRegistry. These new features are more complex, lower-level language concepts.
1, the WeakRef
When an object is assigned to a variable, it points to the block of memory where the object’s value is stored (a strong reference). If the program no longer references the object, the Garbage Collector destroys it and reclaims memory. An instance of WeakRef creates a reference to a given object and returns that object if it is still in memory; Undefined objects are returned if the target object has already been garbage collected.
const obj = { spec: "ES2021" };
const objWeakRef = new WeakRef(obj);
objWeakRef.deref(); // Return undefined if the object obj is reclaimed, otherwise return obj
Copy the code
2, FinalizationRegistry
An instance of FinalizationRegistry fires a callback function after the registered target object is garbage collected.
const obj = { spec: "ES2021" };
const registry = new FinalizationRegistry(value= > {
console.log(`${value}`); // Execute if collected
});
registry.register(obj, "ECMAScript 2021"); // This callback is executed when the object obj is reclaimed
Copy the code
It is worth noting that the official tip is to avoid using WeakRef and FinalizationRegistry as much as possible. The garbage collection mechanism relies on the implementation of JavaScript engines, which may vary from engine to engine or version to version.
To view more
Logical Assignment Operators
Logic, as the name implies, the assignment operator is the logical operators, &&, | | and??) And the assignment operator (=).
?? (null value merge operator – ecMA2020); Example: let a = null?? 2; / / 2
a &&= b; // a = a && b;
a ||= b; // a = a || b;a ?? = b;// a = a ?? b;
Copy the code
To view more
Numeric separators
The readability of numbers decreases as the number length increases. Now, you can use underscores (_, U+005F) to separate groups of numbers, making long numbers more legible. This feature is also well known in Java, Python, Perl, Ruby, Rust, Julia, Ada, C# and other programming languages.
const population = 37 _653_260
Copy the code
To view more