Home  >  Article  >  Web Front-end  >  How to recurse a function to depth n in JavaScript?

How to recurse a function to depth n in JavaScript?

WBOY
WBOYforward
2023-09-05 15:01:021302browse

如何在 JavaScript 中将函数递归到深度 n ?

In JavaScript, a function is considered "curried" if it accepts one or more arguments and returns a new function that takes the remaining arguments. Currying is a powerful technique that can be used to create new functions from existing functions, or "currying" functions of depth n.

Why do we need to cancel function references?

There are several reasons why you might want to uncurry a certain function. For example, you might want -

  • Use curried functions in contexts that require uncurried functions

  • Convert a curried function to a non-curried form to make it easier to read or debug

  • Optimize the performance of curried functions

How to cancel function reference?

There are two waysCurrying functions -

  • The first method is to use the "Function.prototype.apply" method.

  • The second method is to use the "Function.prototype.call" method.

Use the "Function.prototype.apply" method

"Function.prototype.apply" method can be used to store functions of depth n. To do this, you need to simply pass the "this" value and the parameter array to the "apply" method. For example -

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <p>uncurry up to depth 2 using Function apply() method</p>
   <div id="result"></div>
   <script>
      function add(a, b) {
         return a + b;
      }
      function curriedAdd(a) {
         return function(b) {
            return a + b;
         }
      }
 
      // Uncurry the "curriedAdd" function up to depth 2:
      document.getElementById("result").innerHTML = curriedAdd.apply(null, [1]).apply(null, [2]); // 3
   </script>
</body>
</html>

Use the "Function.prototype.call" method

"Function.prototype.call" method can also be used to uncurry a function to depth n. The "call" method is similar to the "apply" method, but you pass the "this" value and arguments to the "call" i> method as separate arguments , rather than as an array. For example -

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <p> uncurry up to depth 2 using Function call() method</p>
   <div id="result"></div>
   <script>
      function add(a, b) {
         return a + b;
      }
      function curriedAdd(a) {
         return function(b) {
            return parseInt(a) + parseInt(b);
         }
      }
 
      // Uncurry the "curriedAdd" function up to depth 2:
      document.getElementById("result").innerHTML = curriedAdd.call(null, [1]).call(null, [2]); // 3
   </script>
</body>
</html>

In the above example, we used the "call" method to curry the "curriedAdd" function. As you can see, the "call" method is slightly more verbose than the "apply" method, but it has the same effect.

What are the benefits of using the "Function.prototype.apply" method?

There are several benefits of using the "Function.prototype.apply" method to dereference a function -

  • The "apply" method is faster than the "call" method.

  • The "apply" method is more widely supported than the "call" method.

  • The "apply" method is more concise than the "call" method.

In summary, currying is a powerful technique that can be used to create new functions from existing functions, or to "curry" functions of depth n.

The above is the detailed content of How to recurse a function to depth n 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