FUNCTION NAMES
Because function names are simply pointers to functions, they act like any other variable containing a pointer to an object. This means it's possible to have multiple names for a single function, as in this example:
function sum(num1, num2) {
return num1 + num2;
}
console.log(sum(10, 10)); // 20
let anotherSum = sum;
console.log(anotherSum(10, 10)); // 20
sum = null;
console.log(anotherSum(10, 10)); // 20
This code defines a function named sum()
that adds two numbers together. A variable, anotherSum
, is declared and set equal to sum
. Note that using the function name without parentheses accesses the function pointer instead of executing the function. At this point, both anotherSum
and sum
point to the same function, meaning that anotherSum()
can be called and a result returned. When sum
is set to null
, it severs its relationship with the function, although anotherSum()
can still be called without any problems.
All function...