ES 2021 is the latest version of the .. the most popular programming language in the world~
There are only a handful of features in this iteration...
For further actions, you may consider blocking this person and/or reporting abuse
Can't one be done with regex /g tag with normal replace function.
Consider a case, where you want to replace all occurrences of a user provided string with something else, maybe an empty string. Since we don't have something like RegExp.escape (yet), it's quite difficult to do it right in vanilla JS.
But with replaceAll, you just do
text.replaceAll(externalString, '')
and you're done.This new method can also have positive effects in terms of performance, since no regular expression had to be parsed and executed for this relatively simple task.
Although there is also another way, which is available today, but it may not be obvious for everyone.
It is the split/join technique:
text.split(replaceThat).join(withThat)
That's probably what's happening under the hood. This new method is just a way to make it easier
It's like
.sub
and.gsub
methods in the Ruby.I don't really understand number 4
From the proposal I believe
a ||= b
is syntactic sugar for:a = a || b
, but is meant to not trigger the object's setter function (if it has one).They use the example:
obj.a = obj.a ?? b
would trigger the setter forobj.a
since it's being reassigned in both instances. To prevent this we could doobj.a ?? (obj.a = b);
which would only trigger the reassignment ifobj.a == null
, but apparently that's weird. Thus to make it succinct they propose:a ??= b
.Which will not trigger the setter unless it needs to.
Thanks!
It's just like an assignment, except that it will only get assigned if a is already false. If it isn't, then no assignment, a will still be what it was.
death-to-ie11.com/
I know this is provocative and off-topic, but IE 11 isn't really relevant now. I have much more issues with Safari, since it is still in use and is often quite problematic.