functions (and different ways of defining them in js)

ref – https://dmitripavlutin.com/6-ways-to-declare-javascript-functions/
https://www.davidbcalhoun.com/2011/different-ways-of-defining-functions-in-javascript-this-is-madness/

A function is a parametric block of code defined one time and called any number of times later.

1) function declaration

A function declaration is made of function keyword. It is followed by an obligatory function name, a list of parameters in a pair of parenthesis (para1, …, paramN) and a pair of curly braces {…} that delimits the body code.

The function declaration creates a variable in the current scope with the identifier equal to function name. This variable holds the function object.

The function variable is hoisted up to the top of the current scope, which means that the function can be invoked before the declaration

Which practically means that, yes, you can call the functions before they’re written in your code. It won’t matter, because the entire function gets hoisted to the top of its containing scope. (This is contrasted with variables, which only have their declaration hoisted, not their contents, as we’ll see in the next section).

Function Expression

A function expression is determined by a function keyword, followed by an optional function name, a list of parameters in a pair of parenthesis (para1, …, paramN) and a pair of curly braces { … } that delimits the body code.

This function object can:
– be referenced by a local variable you declare.
– assigned to a method name on an object
– be used as a callback function

2) unnamed function expression

function expression used as a method name on an object:

Will be executed as this:

Therefore, with functions, the order of setting and calling is important:

function expressions with parenthesis

Here again, we’ll see that if we declare an anonymous function, the function’s name property will be valid to use inside the function. In other words, the function name is only accessible within the function. However, globally, we’ll have to use the reference name.

In the case below, reference name D must be used globally. The function name foo can be used inside the function.

Useful for Recursion
Because the function’s name is accessible in the function itself, this turns out to be useful for recursive functions, much more useful than the plain old anonymous function.

Here’s a trivial recursive function to illustrate calling itself from within the named function expression:

IIFE

first, lets look at a standard example:

Useful for debugging
As a few have pointed out, giving previously anonymous functions names helps in debugging, since the function name shows up on the call stack.

3) Named function Expression

the function object has a property name, which displays the name of the function. It is assigned to a variable. The variable is used in the current scope. But inside the function body, the function name is used.

Also, a named function is created, the “name” property holds the function name.

Inside the function body a variable with the same name holds the function object. The function name (funName) is available inside the function. However, outside, it is not. Outside, it recognizes the variable name which the function was assigned (getType).

4) Shorthand Method definitions

short hand method definitions are used in object literal construction like so:

add() and get() methods in collection object are defined using short method definition. These methods are called as usual: collection.add(…) and collection.get(…).

but why?
The short approach of method definition has several benefits over traditional property definition with a name, colon : and a function expression like so:

– A shorter syntax is easier to read and write
– Shorthand method definition creates named functions, contrary to a function expression. It is useful for debugging.

5) Arrow Functions

An arrow function is defined using a pair of parenthesis that contains the list of parameters (param1, param2, …, paramN), followed by a fat arrow => and a pair of curly braces {…} that delimits the body statements.

note: When the arrow function has only one parameter, the pair of parenthesis can be omitted. When it contains a single statement, the curly braces can be omitted too.

The function declared using a fat arrow has the following properties:

– The arrow function does not create its own execution context, but takes it lexically (contrary to function expression or function declaration, which create own this depending on invocation)

– The arrow function is anonymous: name is an empty string (contrary to function declaration which have a name)
– arguments object is not available in the arrow function (contrary to other declaration types that provide arguments object)

this keyword is one of the most confusing aspects of JavaScript (check this article for a detailed explanation on this).
Because functions create their own execution context, often it is hard to catch the flying around this.

ECMAScript 6 improves this usage by introducing the arrow function, which takes the context lexically. This is nice, because from now on is not necessary to use .bind(this) or store the context var self = this when a function needs the enclosing context.

short callbacks

The parenthesis pairs and curly braces are optional for a single parameter and single body statement. This helps creating very short callback functions.

Originally, given an array, we use Array’s prototype function some to see if there exist a zero in the array. The some() method tests whether at least one element in the array passes the test implemented by the provided function.
If it exists, return 0, if it does not, return false.

However, since there’s only a single parameter, and a single body statement, we can do it shorthand like so:

5) Generator functions

Function declaration

standard declaration of a generator function:

Function expressions

now we assign an anonymous function generator definition to an expression variable. Execute the expression, and call the methods on it.

shorthand

In all 3 cases the generator function returns the generator object g. Later g is used to generated series of incremented numbers.

6 – new Function

Function objects created with the Function constructor are parsed when the function is created. This is less efficient than declaring a function with a function expression or function statement and calling it within your code because such functions are parsed with the rest of the code.

All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.

Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was called. This is different from using eval with code for a function expression.