目錄
#Using nested Function With Name
Syntax
演算法
Example
The MUL() function in JavaScript
透過柯里化函數
首頁 web前端 js教程 解釋一下JavaScript中的MUL()函數

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

Aug 21, 2023 pm 08:37 PM

解釋一下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 id="The-MUL-function-in-JavaScript"> 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 id="The-MUL-function-in-JavaScript"> 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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何創建和發布自己的JavaScript庫? 如何創建和發布自己的JavaScript庫? Mar 18, 2025 pm 03:12 PM

文章討論了創建,發布和維護JavaScript庫,專注於計劃,開發,測試,文檔和促銷策略。

如何在瀏覽器中優化JavaScript代碼以進行性能? 如何在瀏覽器中優化JavaScript代碼以進行性能? Mar 18, 2025 pm 03:14 PM

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

前端熱敏紙小票打印遇到亂碼問題怎麼辦? 前端熱敏紙小票打印遇到亂碼問題怎麼辦? Apr 04, 2025 pm 02:42 PM

前端熱敏紙小票打印的常見問題與解決方案在前端開發中,小票打印是一個常見的需求。然而,很多開發者在實...

誰得到更多的Python或JavaScript? 誰得到更多的Python或JavaScript? Apr 04, 2025 am 12:09 AM

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

如何使用瀏覽器開發人員工具有效調試JavaScript代碼? 如何使用瀏覽器開發人員工具有效調試JavaScript代碼? Mar 18, 2025 pm 03:16 PM

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

如何使用JavaScript將具有相同ID的數組元素合併到一個對像中? 如何使用JavaScript將具有相同ID的數組元素合併到一個對像中? Apr 04, 2025 pm 05:09 PM

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

如何使用源地圖調試縮小JavaScript代碼? 如何使用源地圖調試縮小JavaScript代碼? Mar 18, 2025 pm 03:17 PM

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

神秘的JavaScript:它的作用以及為什麼重要 神秘的JavaScript:它的作用以及為什麼重要 Apr 09, 2025 am 12:07 AM

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

See all articles