Higher-Order Functions
suggest changeIn general, functions that operate on other functions, either by taking them as arguments or by returning them (or both), are called higher-order functions.
One kind of higher-order function takes another function as an argument. Passing a callback uses that pattern.
function iAmCallbackFunction() {
console.log("callback has been invoked");
}
function iAmJustFunction(callbackFn) {
// do some stuff ...
// invoke the callback function.
callbackFn();
}
// invoke your higher-order function with a callback function.
iAmJustFunction(iAmCallbackFunction);
A higher-order function can also return another function.
function iAmJustFunction() {
// do some stuff ...
// return a function.
return function iAmReturnedFunction() {
console.log("returned function has been invoked");
}
}
// invoke your higher-order function and its returned function.
iAmJustFunction()();
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents