-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Higherorder functions in Javascript are known as callback functions. They are functions which are used as an argument in another function or return a function as a result. An simple example could be as follows:
function mainFunction(a, b, callback)
{
var result = callback(a, b);
return result;
}
function callbackFunction(x, y)
{
return x * y;
}
//Call
console.log(mainFunction(2, 2, callbackFunction));//Outputs 4
One thing which is important to understand is that callbackFunction could be defined differently (e.g. return x + y
) and it will still work fine. Following this insight, High-order functions gives the developer a certain flexibility to change or write callback functions as they need it whithin a specific frame.
Another way is to write a callback function as an anonymous function, like this:
function mainFunction(a, b, callback)
{
var result = callback(a, b);
return result;
}
//a = 2 and b = 2
var output = mainFunction
(
2,
2,
function(x,y)
{
return x * y;
}
)
console.log(output)//Outputs 4
The anonymous version could be often seen in jQuery.
Javascript offers already useful Higher-order functions. The following listed are used in this project:
Those six have some properties in comon, like:
STILL IN WORK!
This knowledge was gained:
Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman