解释一下JavaScript中的MUL()函数

WBOY
WBOY 转载
2023-08-21 20:37:02 486浏览

解释一下JavaScript中的MUL()函数

在本教程中,我们将学习在JavaScript中实现MUL()函数。这个函数是一种非常简单的乘法函数。基本上,我们使用嵌套函数的概念来实现MUL()函数。嵌套函数用于创建闭包和装饰器。而且我们可以使用嵌套函数来实现隐私。

There are two ways to implement MUL() function using nested functions −

  • Using nested function with name

  • By currying a function

Using nested Function With Name

在JavaScript中,我们可以使用嵌套函数来获得乘法结果。我们需要编写n个嵌套函数来相乘n个数字。

JavaScript is a First-class function language. It means functions in JavaScript can be treated like any other variable. So, here we return the inner function in the return statement of the outer function.

Syntax

Users can follow the below syntax to implement MUL( ) function using nested functions with name.

function mul(num1){
   function mul1(num2){
      function mul2(num3){
         return num1*num2*num3;
         
      }; // end of mul2()
      return mul2;
      
   }; // end of mul1()
   return mul1;
   
} // end of mul()

For example, we multiply three numbers – num1, num2, and num3. A function is a keyword to define function in JavaScript. Here, we are defining a function with the name mul( ), which has num1 as the parameter. Inside the mul( ) function, we return function mul1( ), which is defined inside mul( ) function. mul1( ) has num2 as parameter, it returns function mul2( ). And mul2( ) has num3 as the parameter; it returns the product of num1, num2, and num3.

这样,我们可以编写n个函数来相乘n个数字。

算法

  • 步骤1 - 使用第一个数字 num1 作为参数定义函数 mul( )。

  • 步骤 1.1 − 在函数 mul( ) 内部,使用第二个数字 num2 作为参数定义函数 mul1( )。

  • 步骤 1.2 - 在函数 mul() 的返回语句中,返回 mul1。

  • Step 2 − Inside function mul1( ), define function mul2( ) with third number num3 as parameter.

  • Step 2.1 − In the return statement of function mul1( ), return mul2.

  • Step 3 − In the return statement of function mul2( ), return a product of num1, num2, and num3

Example

在下面的例子中,我们正在将三个数字相乘。当我们只传递了两个数字时,我们也观察到了输出。

<html>
<body>
<h2> The MUL() function in JavaScript </h2>
<div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      function mul(num1){
         function mul1(num2){
            function mul2(num3){
               return num1*num2*num3;
               
            }; // end of mul2()
            return mul2;
            
         }; // end of mul1()
         return mul1;
         
      } // end of mul()
      output.innerHTML = "Multiplication of 2, 3 and 4 is : ";
      output.innerHTML += mul(2)(3)(4) + "<br><br>";
      output.innerHTML += "Multiplication of 4 and 6 is : ";
      
      //This line returns a function
      output.innerHTML += mul(4)(6) + "<br><br>";
      output.innerHTML += "Multiplication of 3, 5 and 7 is: ";
      
      //Another way of multiplication
      const temp = mul(3)(5);
      output.innerHTML += temp(7);
   </script>
</body>
</html>

在上面的代码中,用户可以看到通过将2、3和4一起传递给函数调用来进行乘法运算。当我们只传递两个数字时,它返回一个函数。然后我们在函数调用中传递3和5,但是我们将结果存储在temp变量中。然后使用temp变量,我们传递7。所以,我们得到了3、5和7的乘积= 105。

Note − We cannot call mul1() or mul2() function outside mul() function.

通过柯里化函数

我们可以通过柯里化函数的方式以另一种方式来编写上述逻辑。当我们无法同时提供所有参数给一个函数时,柯里化是很有用的。那些不会被任何地方调用的函数可以被写成匿名函数。

Syntax

按照以下语法来实现通过柯里化函数来实现MUL( )。

function mul(num1) {
   return function(num2) {
      return function(num3) {
         return num1 * num2 * num3;
      };
   };
}

在这里,我们也以3个数字的例子来说明,这样用户可以观察到我们可以通过编写匿名函数来实现上述逻辑的差异。最外层的函数mul()有一个参数num1;它返回一个带有参数num2的函数。这个函数返回一个带有参数num3的函数。最内层的函数返回num1、num2和num3的乘积。

相同的逻辑可以应用于更多的数字。

算法

  • Step 1 − Define function mul( ) with num1 as a parameter.

  • 步骤 2 − 在函数 mul( ) 的返回语句中,使用 num2 作为参数定义匿名函数(我们称之为第一个匿名函数,以便理解)。

  • Step 3 − In the return statement of 1st anonymous function, define 2nd anonymous function with num3 as a parameter.

  • Step 4 − In the return statement of the 2nd anonymous function, return a product of num1, num2, and num3.

Example

在下面的示例中,我们通过柯里化一个函数来实现MUL()函数。

<html>
<body>
<h2> The MUL() function in JavaScript </h2>
<div id="output"> </div>
   <script>
      let output = document.getElementById("output");
      function mul(num1) {
         return function(num2) {
            return function(num3) {
               return num1 * num2 * num3;
            };
         };
      }
      output.innerHTML = "Multiplication of 2, 4 and 6 is: ";
      output.innerHTML += mul(2)(4)(6) + "<br><br>";
      output.innerHTML += "Output when we pass only 9 is: <br>";
      
      //This line returns a function
      output.innerHTML += mul(9) + "<br><br>";
      output.innerHTML += "Multiplication of 2, 3 and 5 is: ";
      
      //Another way of multiplication
      const temp = mul(2)(3);
      output.innerHTML += temp(5);
   </script>
</body>
</html>

In the above output, users can see that when we pass three numbers in function call, we get the product of 3 numbers. We are getting 48 when we are passing 2, 4, and 6 together in the function call. When we pass only 9, we get function. Then we pass 2 and 3 only in a function call and store the result in the temp variable. And using that temp variable, we are passing 5. So, we get the product of 2, 3, and 5 = 30.

We have learned the implementation of the MUL( ) function with two different methods: nested functions and currying a function.

以上就是解释一下JavaScript中的MUL()函数的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除