Home  >  Article  >  Web Front-end  >  How to call a function that returns another function in JavaScript?

How to call a function that returns another function in JavaScript?

WBOY
WBOYforward
2023-09-02 21:21:021148browse

如何在 JavaScript 中调用一个返回另一个函数的函数?

We will call a function by quoting the function name and adding parentheses after it. if The function we call returns another function (in our case it does) and we need to assign into a variable or call it immediately. In the future, we need to ensure that we Understand the behavior of the returned function and how to use it in our code. This is called function currying.

Function currying

  • Function currying is a technique in functional programming in which a function is transformed into a series of functions, each of which accepts a parameter.

  • This allows partial application of a function's arguments and can simplify function composition.

  • It is named after logician Haskell Curry.

  • In JavaScript, the "curry" function can be used to curry a given function.

method

In JavaScript, you can call a function that returns another function by first assigning the returned function to a variable and then calling it using the variable name followed by parentheses.

Example

let outerFunction = function() {
   return function() {
      console.log("Inner function");
   }
}
let innerFunction = outerFunction();
innerFunction(); // "Inner function"

Output

Inner function

You can also call the inner function immediately after calling the outer function by adding parentheses in the outer function call as follows -

outerFunction()(); // "Inner function"

You can also use arrow functions instead of functions -

let outerFunction = () => () => console.log("Inner function");
let innerFunction = outerFunction();
innerFunction(); // "Inner function"

Output

Inner function

or

outerFunction()(); // "Inner function"

Both will give the same result

Final code

This is an example of calling a function that returns another function in JavaScript -

// A function that returns another function
function createMultiplier(x) {
   return function(y) {
      return x * y;
   };
}

// Call the createMultiplier function and assign the returned function to a variable
let double = createMultiplier(2);

// Use the returned function to perform a calculation
console.log(double(5)); // Output: 10

illustrate

  • The createMultiplier function accepts a single parameter x and returns a new function. This return function accepts a single argument y and returns the product of x and y.

  • We call the createMultiplier function and pass in the value 2 as a parameter, and the function assigns the returned function to the variable double.

  • Now the double variable is a function that accepts argument y and returns x*y (where x is 2).

  • We call double(5), which will return 2*5 = 10.

In this example, createMultiplier is a higher-order function because it returns a function. The returned function is called a closure because it remembers the value of x from the scope of the outer function.

Output

10

The above is the detailed content of How to call a function that returns another function in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete