This is the fourth day of my participation in Gwen Challenge

A quick look at ECMAScript’s relationship to JavaScript. ECMAScript is the language specification, and JavaScript is an implementation of that specification. We often think of JavaScript as equivalent to ECMAScript, but a complete JavaScript implementation actually consists of three parts: the core (ECMAScript), the Document Object Model (DOM), and the Browser Object Model (BOM).

When we have a problem developing a project in JavaScript, we can usually use Google or Stack Overflow to get the answer. But when we can’t get the results we want by conventional means, it’s time to look up the specification. Try a global search on ECMA-262

ES6 has a relatively large change, I suggest ruan Yifeng “ES6 Introduction tutorial” for detailed study. After ES6, ECMAScript adds a small number of new features each year. For us, half an hour a year is enough to fully learn and master these new features.

For details about ES7 to ES11, see internal parameters.

ES12

String.prototype.replaceAll

This method replaces all characters in the string that match the rules and returns a brand new string.

let str = 'Hello, world';
str.replaceAll('l'.' '); // "Heo, word"
/ / equivalent to
str.replace(/l/g.' ');
Copy the code

Note that if the matching rule of the method is regular expression, the G identifier must be used. Otherwise, an error will be reported.

str1.replaceAll(/l/.' ');  // TypeError: String.prototype.replaceAll called with a non-global RegExp argument
Copy the code

Promise.any

Accept an array of all promises. If any of the promises succeed, resolve returns the result of the first resolve, and if all fail, an exception is thrown.

Promise.any([
  new Promise((resolve, reject) = > setTimeout(reject, 1000.'I'm the first promise.')),
  new Promise((resolve, reject) = > setTimeout(resolve, 2000.'I'm the second promise')),
  new Promise((resolve, reject) = > setTimeout(resolve, 3000.'I'm the third promise.')),
])
.then(value= > console.log(value)) // I'm the second promise
.catch (err= > console.log(err))

//---------------------
Promise.any([
  Promise.reject(),
  Promise.reject(),
  Promise.reject()
])
.then(value= > console.log(value))
.catch (err= > console.log(err)) //AggregateError: All promises were rejected
Copy the code

WeakRefs

The proposal includes two major new features:

  • Use the WeakRef class to create weak references to objects
  • After the object is garbage collected using the FinalizationRegistry class, a user-defined finalizer is run

They can be used separately or together.

A WeakRef object contains a weak reference to the object, which is called the target or referent of the WeakRef object. A weak reference to an object means that it does not prevent the GC from collecting when the object should be collected by the GC.

let ref = new WeakRef(obj)
let isLive = ref.deref() // If obj is garbage collected, isLive is undefined
Copy the code

FinalizationRegistry capability: When an object registered in the registry is reclaimed, a cleanup callback is requested at some point in time.

const registry = new FinalizationRegistry(heldValue= > {
  / /...
});

// Use the register method to register any object you want to clean up the callback, passing in that object and heldValue
registry.register(theObject, "some value");
Copy the code

Logical Assignment Operators

Three new logical assignment operators are added, as follows:

a ||= b;
/ / equivalent to the
a || (a = b);

a &&= b;
/ / equivalent to thea && (a = b); a ?? = b;/ / equivalent to the
a ?? (a = b);
Copy the code

Numeric Separators

Visual separators can be added between numbers to increase readability by separating numbers with underscores.

let budget = 1 _000_000_000_000;
budget === 10六四屠杀12; // true

let nibbles = 0b1010_0001_1000_0101;
console.log(!! (nibbles & (1 << 7))); // true
Copy the code