An anonymous function is a function without a name! Anonymous functions are commonly assigned to a variable name or used as a callback function.
Syntax for anonymous function:
Austensibly an anonymous function is an expression, and not a declaration. When we add a name before the parameter in the first example above, it becomes a declaration and would be freestanding. As such it would be hoisted at load time. Expressions are never hoisted and only get picked up on the second pass through the code by the parsing engine. Unless they are callbacks in an iterator or event handler they are always assignments and get bound to a variable.
If we use const then it becomes a permanent function. If on the other hand we use var or let our expression can be replaced. That means we can assign new functions to a variable (just donât try to use let again, and we should know by now not to redeclare variables, so var is off the table, too). A simple assignment is all that is needed to replace an expression.
Consider what might be done with this constructâŚ
lib = [x => --x, x => x, x => ++x]
This run took 275 iterations (of which 100 were zero sum) to play out. It might never have, thatâs the risk we took, but it did.
x = 9;
while (x > 0) {
x = lib[Math.floor(Math.random() * 3)](x); // function expression invoked
console.log(x);
}
In case youâre wondering how I got that number, the left column contains repeat counts. I dropped the tabulated data into Excel and inserted a formula in the last, rightmost cell after labeling the left column âzerosâ (sans the âdefinedâ)âŚ
lib is an arbitrary name given to the an array of function expressions. The line above accessess a random index of the array (thatâs what [] is for) and invokes the function on the parameter, x.
Yes, but the function still does not have a name, and it is an expression, not a hoistable function. A variable is a name for a given reference, which reference may be any object, such as a function expression. Assignments are from right to left. The object on the right is an anonymous function. No further semantics involved.
Because they are expressions, we can write/insert them anywhere a value would appear.
Nothing is implied. Hoisting is simply assignment of scope, and declared variables go to the top of their respective scope making them accessible even if they are called before the line in which they are declared.
console.log(foo('foobar')) // call before declaration
function foo (bar) {
return bar.toUpperCase()
}
Console Output
FOOBAR
This only works for declared functions, not function expressions which cannot be called before they are declared.
Hey man, really appreciate your contribution to the forum. But sometimes your explanations are quite verbose and full of technical jargon. Obviously the concepts are very technical but what I understand that folks on this platform are very noobs in programming. For the question - what is an anonymous function you went into hoisting , lib, variable declaration etc. Which I think makes the reader more confused.
Thatâs one opinion. Itâs the humble opinion of this noob that we need more contributors like @mtf to keep things highly challenging and interesting. I like his âextra studyâ posts and that he often provides supplemental information. This is an educational forum and I personally have learned more from mtfâs objective contributions than from those of any other single community member thus far. I find mtfâs style thought provoking which only serves to elevate this forum and prevents topics from getting too watered down.