Friday, June 25, 2021

ES2021 Features

ES2021 Features!

Logical Assignment Operators

Logical Assignment Operators 📖.


x ||= y;
x || (y = z);


x &&= y;
x && (y = z);


x ??= y;
x ?? (y = z);
const updateID = user => {

  
  if (!user.id) user.id = 1

  
  user.id = user.id || 1

  
  user.id ||= 1
}
function setOpts(opts) {
  opts.cat ??= 'meow'
  opts.dog ??= 'bow';
}

setOpts({ cat: 'meow' })

Numeric Separators

Visual separation between groups of digits 📖

1_000_000_000           
101_475_938.38          

let fee = 123_00;       
let fee = 12_300;       
let amount = 12345_00;  
let amount = 123_4500;  
let amount = 1_234_500; 
0.000_001 
1e10_000  
0xA0_B0_C0;

Promise.any and AggregateError

Promise.any + AggregateError 📖

Promise.any([
  fetch('https://v8.dev/').then(() => 'home'),
  fetch('https://v8.dev/blog').then(() => 'blog'),
  fetch('https://v8.dev/docs').then(() => 'docs')
]).then((first) => {
  
  console.log(first);
  
}).catch((error) => {
  
  console.log(error);
});

^ In the above example error is an AggregateError

String.prototype.replaceAll

replaceAll 📖



'x'.replace('', '_');


'xxx'.replace(/(?:)/g, '_');


'xxx'.replaceAll('', '_');

WeakRefs and FinalizationRegistry Objects

WeakRefs and FinalizationRegistry 📖

let target = {};
let wr = new WeakRef(target);





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

registry.register(myObject, "some value", myObject);

registry.unregister(myObject);

With ♥ Hemanth.HM


from Hacker News https://ift.tt/3j28J4U

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.