Javascript-Higher-Order Functions-Day3
Javascript-Higher-Order Functions-Day3
Introduction
When we speak to other humans, we share a vocabulary that gives us quick ways to
communicate complicated concepts. When we say "bake", it calls to mind a familiar
subroutine— preheating an oven, putting something into an oven for a set amount of
time, and finally removing it. This allows us to abstract away a lot of the details and
communicate key concepts more concisely. Instead of listing all those details, we can
say, "We baked a cake," and still impart all that meaning to you.
We're also going to learn about another way to add a level of abstraction to our
programming: higher-order functions.Higher-order functions are functions that accept
other functions as arguments and/or return functions as output. This enables us to build
abstractions on other abstractions, just like "We hosted a birthday party" is an
abstraction that may build on the abstraction "We made a cake."
In summary, using more abstraction in our code allows us to write more modular code
which is easier to read and debug.
Functions as Data
JavaScript functions behave like any other data type in the language; we can assign
functions to variables, and we can reassign them to new variables.
Below, we have an annoyingly long function name that hurts the readability of any code
in which it's used. Let's pretend this function does important work and needs to be
called repeatedly!
What if we wanted to rename this function without sacrificing the source code? We can
re-assign the function to a variable with a suitably short name:
const busy = announceThatIAmDoingImportantWork;
busy is a variable that holds a reference to our original function. If we could look up the
address in memory of busyand the address in memory
of announceThatIAmDoingImportantWork they would point to the same place. Our
new busy() function can be invoked with parentheses as if that was the name we
originally gave our function.
In JavaScript, functions are first class objects, this means that like other objects you've
encountered, JavaScript functions can have properties and methods.
Functions are special because we can invoke them, but we can still treat them like any
other type of data. Let's get some practice doing that!
Instruction
Functions as Parameters
Since functions can behave like any other type of data in JavaScript, it might not surprise
you to learn that we can also pass functions (into other functions) as parameters.
A higher-order function is a function that either accepts functions as parameters, returns
a function, or both! We call the functions that get passed in as parameters and
invoked callback functions because they get called during the execution of the higher-
order function.
timeFuncRuntime(addOneToOne);
This higher-order function could be used with any callback function which makes it a
potentially powerful piece of code.
We then invoked timeFuncRuntime() first with the addOneToOne() function - note
how we passed in addOneToOneand did not invoke it.
timeFuncRuntime(() => {
for (let i = 10; i>0; i--){
console.log(i);
}
});
Let’s get some practice using functions and writing higher-order functions.
Instruction
Review
Great job! By thinking about functions as data and learning about higher-order
functions, you've taken important steps in being able to write clean, modular code and
take advantage of JavaScript's flexibility.