Here are two ways to fix it that involves adding more parentheses: (function foo() )() and (function foo() ()). Statements that begin with function are considered to be function declarations; by wrapping this function within (), it becomes a function expression which can then be executed with the subsequent (). These functions are not exposed in the global scope and you can even omit its name if you do not need to reference itself within the body.
Hoisting is a term used to explain the behavior of variable declarations in your code. Variables declared or initialized with the var keyword will have their declaration "moved" up to the top of their module/function-level scope, which we refer to as hoisting. However, only the declaration is hoisted, the assignment (if there is one), will stay where it is.
Variables declared via let and const are hoisted as well. However, unlike var and function, they are not initialized and accessing them before the declaration will result in a ReferenceError exception. The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.
The former is a function declaration while the latter is a function expression. The key difference is that function declarations have its body hoisted but the bodies of function expressions are not (they have the same hoisting behavior as variables). For more explanation on hoisting, refer to the question above on hoisting. If you try to invoke a function expression before it is defined, you will get an Uncaught TypeError: XXX is not a function error.
Each variable declaration defines a scope for that variable,that is, the section(s) of the program in which this variable isdefined and usable. In the example above, the scope of the variable\(x\) is the body of the function. When the use of a variable, say\(x\), appears within the scope of a declaration of \(x\), wesay that the former is bound to the latter, and the latter isthe binding occurrence of the variable. So, in the exampleabove, the declaration of \(x\) in parentheses is the bindingoccurrence of this variable and the use \(x\) on the next line isbound to this binding occurrence. A variable that is not bound is saidto be free. In the example above, the occurrence of \(y\)is free in the body of the function, since this function does notcontain any binding occurrence (i.e., declaration) of \(y\).
This lambda expression is a lambda abstraction whose parameter is\(y\) and whose body is the application of the identity functionto the expression \((y\ x)\). Therefore, the \(y\) after the\(\lambda\) is the binding occurrence of the variable\(y\). The scope of this declaration is \((\lambda x.x\ (y\x))\), which implies that the rightmost occurrence of \(y\) isbound to the leftmost binding occurrence. In contrast, the scope of thebinding occurrence of \(x\) in \(\lambda x.x\) is just thesecond \(x\) in it (that is, as always, the body of the lambdaabstraction). As a result, the third, rightmost occurrence of \(x\)in the expression above is free: it is a use of \(x\) thatdoes not belong to the scope of any declarations of \(x\).
var declarations are scoped to the beginning of the nearest enclosingfunction, script or module, which can cause unexpected behavior, especially withfunction closures that reference var declarations inside of loops. Thefollowing code gives an example:
Even though var declarations are scoped to the beginning of the enclosingfunction, var declarations should be as close as possible to their first use,for readability purposes. However, do not put a var declaration inside a blockif that variable is referenced outside the block. For example:
Programmers often used the words invocation and invoking when talking about function and method calls. The terms are synonymous with "call" and "calling" but more clearly distinguish between the noun and verb forms. Invocation is a noun while invoking (as well as invoke and invoked) are the verb forms. You invoke a function to call it or write a function invocation that will be called when the program runs. We also use these terms, especially when we need to avoid ambiguity.
Any function definition that doesn't have the word function at the very beginning of a statement is a function expression. Even wrapping what looks like a function declaration in parentheses creates a function expression:
For functions, if a library name is not provided, the invocation must refer to a locally defined function, or a CQL system function. Function resolution proceeds by attempting to match the signature of the invocation, i.e. the number and type of each argument, to a defined signature for the function. Because the CQL type system supports subtyping, generics, and implicit conversion and casting, it is possible for an invocation signature to match multiple defined signatures. In these cases, the least converting signature is chosen, meaning the signature with the fewest required conversions. If multiple signatures have the same number of required conversions, an ambiguous resolution error is thrown, and the author must provide an explicit cast or conversion to resolve the ambiguity.
When CoffeeScript was designed, var was intentionally omitted. This was to spare developers the mental housekeeping of needing to worry about variable declaration (var foo) as opposed to variable assignment (foo = 1). The CoffeeScript compiler automatically takes care of declaration for you, by generating var statements at the top of every function scope. This makes it impossible to accidentally declare a global variable.
This is to avoid grammatical ambiguity, since in CoffeeScript such a construct looks identical to a function call (e.g. get(function foo() )); and because there is an alternate syntax that is slightly more verbose but just as effective: