1. Numeric Separators

You can create visual separators between numbers, separated by underscores, to make them more readable

const money = 1 _000_000_000;

/ / equivalent to the
const money = 1000000000

1 _000_000_000= = =1000000000;   // true
Copy the code





2, String. Prototype. ReplaceAll ()

Returns a brand new string, with all characters matching the rules replaced

const str = 'What a hard-working Chetoo, chetoo, chetoo, you're hard-working and good-looking.';
const res = str.replaceAll('Cheettozi'.'Front end');

// res => "You are a hard worker, a worker, a worker, you are hard-working and good-looking"
Copy the code

But if you use an identifier other than g in replaceAll(), or if you do not add g, you will get an error, for example:

const res = str.replaceAll(/ Chettuzai /.'Front end');   
// TypeError: String.prototype.replaceAll called with a non-global RegExp argumentat String.replaceAll

// if g is added, no error will be reported
const res = str.replaceAll(/ Che Tu zai /g.'Front end');
Copy the code

So this is just grammatical sugar for G…

Now that we know how it works, let’s try implementing replaceAll()

  • My_replaceAll ();
String.prototype.my_replaceAll = function(s1,s2){
    G indicates that a global match is performed, and m indicates that multiple matches are performed
    return this.replace(new RegExp(s1,'gm'),s2)
}

const res = str.my_replaceAll(/ Chettuzai /.'Front end');
// res ==> "What an industrious front boy, front boy, front boy, you are hardworking and good-looking"
Copy the code
  • Implementation method two, first split, then join method
const str = 'What a hard-working Chetoo, chetoo, chetoo, you're hard-working and good-looking.';
const res = str.split('Cheettozi').join('Front end');

// res ==> "What an industrious front boy, front boy, front boy, you are hardworking and good-looking"
Copy the code





3. Logical assignment operators

Using the following operators, it is much more elegant and convenient to write short chain expressions in the future


And And equals (&&=)

Assignment is performed only if the left-hand operand is true

var a = 1
var b = 2
a &&= b       //  2  


// This is equivalent to the following two ways
if(a){
    a = b;
}

a = a && (a = b)
Copy the code


Or or equals (||=)

| | = and && =, on the other hand, only when the left operand is false to perform the assignment

var a = undefined
var b = 2
a ||= b         / / 2


// This is equivalent to the following two ways
if(! a) { a = b; } a = a || (a = b)Copy the code


Question question equals (?? =)

?? = Perform assignment only if the left-hand operand is null or undefined

let a = undefined;
let b = 2;

a ?? = b;
console.log(a); / / 2

// This is equivalent to the following two ways
if(a === null && a === undefined ){
    a = b;
}

a = a ?? (a = b)
Copy the code

Matters needing attention:

a = a || b; 	/ / with a | | = b inequitable
a = a && b; 	// is not equivalent to a &&= b
a = a ?? b; 	/ / with a???? B is not equivalent
Copy the code

Do a problem, feel the various?? Fear of domination

Fun q&A time:

let a;
let b = "Unknown front-end"
let c = null;
let d = 0;
lete; e ?? = a ? . b ?? c ?? d ? . a ?? b;console.log(e)
Copy the code

What are the results? SAO years?

Are programmers, can have bad intentions!





4, Promise. Any)

Promise.any() takes a Promise iterable and returns the Promise that succeeded as soon as one of the promises succeeds.

If none of the iterables succeeds (i.e. all promises fail/reject), a failed promise is returned

The opposite of promise.all

// One failure, one success, return the promise of success
Promise
  .any([
    Promise.reject('I've failed, it's my fault! '),
    Promise.resolve('I did it, Ollie.')
 ])
 .then(res= > console.log(res))
 .catch(err= > console.error(err));
This is a big pity. I will succeed, Ollie



// If both fail, a failed promise is returned
Promise
	.any([
      Promise.reject('I've failed, it's my fault! '),
      Promise.reject('I've failed, hit me! '),
])
.then(res= > console.log(res))
.catch(res= > console.log(res))
// AggregateError: All promises were rejected 



// Both are successful
Promise
  .any([
    Promise.resolve('Success Number one, unknown front end'),
    Promise.resolve('Success Number two, unknown front end')
 ])
 .then(res= > console.log(res))
 .catch(err= > console.error(err));
// Success 1, unknown front end
Copy the code





5、 WeakRefs

WeakRef is short for Weak References, and its main purpose is to make Weak References to another object.

This means that it does not prevent the GARBAGE Collector (GC) from collecting objects. This is useful when we don’t want to keep objects in memory forever.

const newRef = new WeakRef({
     name: 'Unknown Chettuzai'.age: '66'.sex: 'male'
});

const obj = newRef.deref();

console.log(obj); // {name: "unknown ", age: "66", sex:" male "}
Copy the code

For the use of WeakRef object should be carefully considered, can not use as far as possible do not use