首頁 > web前端 > js教程 > 主體

解釋一下JavaScript中的MUL()函數

WBOY
發布: 2023-08-21 20:37:02
轉載
1449 人瀏覽過

解釋一下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( ) parameter as returns function mul2( ). And mul2( ) has turns as reparameter 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. 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, 33 , 和 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.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!