Home > Web Front-end > JS Tutorial > body text

How to call a function with a section appended to parameters in JavaScript?

WBOY
Release: 2023-08-25 12:41:05
forward
1344 people have browsed it

如何在 JavaScript 中调用带有附加到参数的部分的函数?

In JavaScript, you can call a function by passing parameters to it. However, sometimes it is necessary to call a function with some parameters already populated. This can be done using a technique called partial function application.

What is partial function application?

Partial function application is a way to create a new function by "pre-populating" some parameters of an existing function. This is useful when you want to create a new function that is similar to an existing function but has some parameters set.

For example, suppose you have a function that calculates the area of ​​a rectangle. The function takes two parameters - the width and height of the rectangle.

Now imagine you want to create a new function to calculate the area of ​​a square. This new function will be similar to the existing function, but it will only require one parameter - the sides of the square.

This is where partial function applications come in. You can use the partial function app to create a new function that is a "partial" version of an existing function. This new function will have the first parameter (width) already set, and only require the second parameter (height).

How to use partial function application in JavaScript?

There are a few different ways to use partial function applications in JavaScript.

method 1

The most common method is to use the bind() method.

The bind() method is a way to create a new function from an existing function. The bind() method takes two parameters: the function to be bound, and the value to be bound to the first argument of the function.

For example, suppose you have the following function -

function add(a, b) { 
   return a + b; 
}
Copy after login

You can use the bind() method to create a new function with the first parameter (a) set.

var add5 = add.bind(null, 5);
Copy after login

Now the add5 function can be used like this -

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      function add(a, b) {
         return a + b;
      }
      var add5 = add.bind(null, 5);
      document.getElementById("result").innerHTML = add5(10)
   </script>
</body>
</html>
Copy after login

As you can see, the first parameter (5) is set and the second parameter (10) is passed to the add5 function.

Method 2

Another way to use partial function application is to create a function that accepts an existing function and a value and returns a new function with the first argument of the existing function set.

For example, you can write a function like this -

function bind(fn, val) { 
   return function() { 
      return fn.apply(null, 
      [val].concat(Array.prototype.slice.call(arguments)));
   };
}
Copy after login

This function accepts an existing function and a value and returns a new function whose value is bound to the first argument of the existing function.

For example, you can use the bind() function to create a new function like this -

var add5 = bind(add, 5);
Copy after login

Now the add5 function can be used like this -

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      function add(a, b) {
         return a + b;
      }
      function bind(fn, val) {
         return function() {
            return fn.apply(null, [val].concat(Array.prototype.slice.call(arguments)));
         };
      }
      var add5 = add.bind(add, 5);
      document.getElementById("result").innerHTML = add5(10)
   </script>
</body>
</html>
Copy after login

As you can see, the first parameter (5) is set and the second parameter (10) is passed to the add5 function.

Partial function application is a useful technique for creating new functions from existing functions. It can be used to create a function that is similar to an existing function, but with some parameters already set.

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

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template