Найти в Дзене

Higher-order finctions in JavaScript

JavaScript Tutorial: Learn JavaScript For Free | Codecademy

Introduction

When we say the word “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.

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.”

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.
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.

To rename this function without sacrificing the source code, we can re-assign the function to a variable with a suitably short name. Notice how we assign announceThatIAmDoingImportantWork without parentheses as the value to the busy variable.
To rename this function without sacrificing the source code, we can re-assign the function to a variable with a suitably short name. Notice how we assign announceThatIAmDoingImportantWork without parentheses as the value to the busy variable.

busy is a variable that holds a reference to our original function.

Functions as Parameters

As you know, a parameter is a placeholder for the data that gets passed into a function.

A higher-order function is a function that either accepts functions as parameters, returns a function, or both! We call functions that get passed in as parameters callback functions. Callback functions get invoked during the execution of the higher-order function.

When we invoke a higher-order function, and pass another function in as an argument, we don’t invoke the argument function. Invoking it would evaluate to passing in the return value of that function call. With callback functions, we pass in the function itself by typing the function name without the parentheses:
When we invoke a higher-order function, and pass another function in as an argument, we don’t invoke the argument function. Invoking it would evaluate to passing in the return value of that function call. With callback functions, we pass in the function itself by typing the function name without the parentheses:

We wrote a higher-order function higherOrderFunc that accepts a single parameter, param. Inside the body, param gets invoked using parentheses. And finally, a string is returned, telling us the name of the callback function that was passed in.

In this example, we invoked higherOrderFunc() with an anonymous function (a function without a name) that counts to 10. Anonymous functions can be arguments too!
In this example, we invoked higherOrderFunc() with an anonymous function (a function without a name) that counts to 10. Anonymous functions can be arguments too!

/* Вызов функции происходит через приписывание в конце идентификатора скобочки "()". Ранее, когда мы сокращали название функции через присвоение её идентификатора константе, мы добавляли к совокупности указателей, ссылающихся на функцию, ещё одно имя. Написание скобочек запустило бы выполнение функции, что не требовалось в момент расширения пространства указателей. */

-5
/home/ccuser/workspace/higher-order-functions-functions-as-parameters-revised/app.js:7   let checkB = func(val);
/home/ccuser/workspace/higher-order-functions-functions-as-parameters-revised/app.js:7 let checkB = func(val);