Global methods
The global JavaScript methods can be used without referring to the built-in object they are part of. This means that we can just use the method name as if it is a function that has been defined inside the scope we are in, without the "object" in front of it. For example, instead of writing:
let x = 7;
console.log(Number.isNaN(x));
You can also write:
console.log(isNaN(x));
So, the Number
can be left out, because isNaN
is made globally available without referring to the class it belongs to (in this instance, the Number
class). In this case, both of these console.log
statements will log false
(they are doing the exact same thing), because isNaN
returns true
when it isn't a number. And 7
is a number, so it will log false
.
JavaScript has been built to have these available directly, so to achieve this, some magic is going on beneath the surface. The JavaScript creators chose the methods that they thought were most common. So the...