There's a relatively new (but safe to use, because you don't care about IE anymore) native JS method to deep-copy (also referred to as "cloning") objects: structuredClone()
.
For example:
myDeepCopy = structuredClone(myObject);
There's a relatively new (but safe to use, because you don't care about IE anymore) native JS method to deep-copy (also referred to as "cloning") objects: structuredClone()
.
For example:
In async functions in JavaScript, returning the result from an async function (i.e. a Promise object) is the same as returning the fulfilled value from that Promise. In code, assuming innerFn
is async, this:
is the same as this:
As with most things in JavaScript, there's a workaround to access the elements on a "closed" shadow DOM: hijacking the attachShadow
method:
The 2-digit
option in the Intl.DateTimeFormat
API doesn't always do what you might expect. In particular, this piece of code doesn't produce a 2-digit number:
However, it turns out this is not strictly a bug.
Using typeof
to check if a variable is undefined is safer than comparing it against undefined
.
setTimeout usually takes a function and a delay argument, but it can also accept more arguments that are passed to the given function. This can make code easier to read, at the cost of having yet another way of doing the same thing.
You can import ES6-style JavaScript modules dynamically using await import(filename);
. For example:
You can send a request when a page is closed, either using the Fetch API with the keepalive
option:
Or the Beacon API:
There is no JavaScript event for a URL change. There is one, however, for when the fragment changes (the part after the #
symbol), called hashchange
, and there is another, popstate
, which doesn't always get triggered, for when the user clicks on the back or forward buttons or the history.back()
or history.go()
methods are called.
The way the DOM works (which represents an HTML page in memory), text and tags such as <div>
are both represented by "nodes" of different types organized in a tree structure so that, for example, text nodes become the "children" of element nodes.
A normalized DOM tree means that there are no empty text nodes or adjacent text nodes. The Node
object has a normalize()
method that makes sure of this.
Copyright 2019 · All rights reserved