2. In computer science, a programming language is said to support first-class functions
(or function literals) if it treats functions as first-class objects. Specifically, this means
that the language supports constructing new functions during the execution of a
program, storing them in data structures, passing them as arguments to other
functions, and returning them as the values of other functions.
A function is an instance of the
Object type
A function can have properties and
has a link back to its constructor
method
You can store the function in a
variable
You can pass the function as a
parameter to another function
You can return the function from a
function
3. Since JavaScript is single-threaded, meaning that only one operation happens at a
time, each operation that’s going to happen is queued along this single thread. The
strategy of passing in a function to be executed after the rest of the parent
function’s operations are complete is one of the basic characteristics of languages
that support higher-order functions. It allows for asynchronous behavior, so a script
can continue executing while waiting for a result. The ability to pass a callback
function is critical when dealing with resources that may return a result after an
undetermined period of time.
This is very useful in a web programming
environment, where a script may send an Ajax
request off to a server, and then need to
handle the response whenever it arrives.
Node.js frequently uses callbacks to make the
most efficient use of server resources. This
approach is also useful in the case of an app
that waits for user input before performing a
function.
4.
5. What is a Function Declaration?
A Function Declaration defines a named function variable without requiring
variable assignment. Function Declarations occur as standalone constructs and
cannot be nested within non-function blocks. It’s helpful to think of them as siblings
of Variable Declarations. Just as Variable Declarations must start with “var”,
Function Declarations must begin with “function”.
The function name is visible within it’s scope and the scope of it’s parent which is
good because otherwise it would be unreachable.
function bar() {
return 3;
}
bar() //3
bar //function
6. What is a Function Expression?
A Function Expression defines a function as a part of a larger expression syntax
(typically a variable assignment ). Functions defined via Functions Expressions
can be named or anonymous. Function Expressions must not start with
“function”, hence the parentheses around the self invoking example below.
//anonymous function expression
var a = function() {
return 3;
}
//named function expression
var a = function bar() {
return 3;
}
//self invoking function expression
(function sayHello() {
alert("hello!");
})();
7. Wait, what’s Hoisting?
Function declarations and function variables are always moved (‘hoisted’) to the top
of their JavaScript scope by the JavaScript interpreter.
When a function declaration is hoisted the entire function body is lifted with it, so
after the interpreter has finished with the code on the right it runs more like this:
function foo(){
function bar() {
return 3;
}
return bar();
function bar() {
return 8;
}
}
alert(foo());
function foo(){
//define bar once
function bar() {
return 3;
}
//redefine it
function bar() {
return 8;
}
//return its invocation
return bar(); //8
}
alert(foo());
8.
9. An immediately-invoked function expression (or IIFE, pronounced "iffy") is a
JavaScript design pattern which produces a lexical scope using JavaScript's
function scoping. Immediately-invoked function expressions can be used to avoid
variable hoisting from within blocks, protect against polluting the global
environment and simultaneously allow public access to methods while retaining
privacy for variables defined within the function. This pattern has been referred to
as a self-executing anonymous function, but Ben Alman introduced the term IIFE
as a more semantically accurate term for the pattern, shortly after its discussion
arose on comp.lang.javascript.
10. (function () { // logic here })();
We see below that this is just an anonymous function declaration enclosed in a
set of parentheses. So if we want to invoke this new function expression right
away we just need to tack a couple of parentheses on the end.
Remove the lead parenthesis and the interpreter will ‘see’ an un-callable
function declaration. Remove the trailing parentheses and nothing will
happen… no execution! Passing variables into the scope is done as follows:
(function(a, b) { … })("hello", "world");
11. // IIFE - Immediately Invoked Function Expression
(function($, window, document) {
// The $, window, document are now locally scoped
// The rest of your code goes here!
}(window.jQuery, window, document));
// The global jQuery object is passed as a parameter
This example of the IIFE module pattern passes in three global variable to the
function so that they are now locally scoped. This is more efficient for
accessing these objects in real-time… an often used trick!