Higher-order functions, what does that even mean.
JavaScript is a functional programing language at least it
has some of the core components from the functional langugage paradigm.
See reference Wiki.
Is it important to know that JavaScript supports Higher-order functions
and treats functions as First-class citizens. We don’t have to know computer science to write code.
But we are still exposed to these concepts in our daily JavaScript code
and it’s the reason why we can pass functions as arguments, store them in variables,
create functions inside a functions body and return them.
Just like we do with any other objects like Strings or Arrays.
Callback functions are a big deal of JavaScript. It’s a a technique
that allows JavaScript to run asynchronously. Callbacks are a direct result of Higher-order
functions, a function that accepts another function as a parameter.
The most used example for introducing callback
is the setInterval function.
After three seconds the anonynous function will fire from the callback que and alert the message.
During thoose three seconds our runtime can continue execute other callbacks or events.
Lets cover some more usage of functions.
indexFunction is a function expression that are assigned to a variable and can be invoked on indexFunction. The function takes one parameter
called changes. The function then executes the forEach method on each object in our parameter
and calls an anonymous function as a callback on each object found. Which logs to our console the
index of found objects.
A pretty useless and confusing function so far.
Then we call the method Array.observe with three parameters:
indexFunction is the callback. It's passed in as a parameter just like any other object. The function will stay in memory and will fire or "Call back" on every new change to arr.
This is all possible because of how functions gets treated in JavaScript.
This post was written.
getTime returns a function that return the current time and date. We can then use the getTime as a value for the property time.
There is more to functions in JavaScript but we have cleared out some of the concepts and explained why we can treat functions in JavaScript the way we do.
I hope you found this article helpful and feel free to comment below.
Enjoy your code.