JavaScript 中的引數和實參。


函式引數是函式定義中存在的變數的名稱。函式實參是傳遞給函式並由它們接收的實際值。

以下是顯示 JavaScript 中的引數和實參的程式碼 −

示例

 線上演示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .result,.sample{
      font-size: 20px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>Function parameters and arguments</h1>
<div class="sample"></div>
<div style="color: green;" class="result"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to pass the above values as arguments to the function.
</h3>
<script>
   let sampleEle = document.querySelector('.sample');
   let resEle = document.querySelector(".result");
   let arr = [22,33,44,55];
   sampleEle.innerHTML = arr;
   function add(ARR){
      let sum = 0;
      ARR.forEach(element => {
         sum+=element;
      });
      return sum;
   }
   document.querySelector(".Btn").addEventListener("click", () => {
      resEle.innerHTML = 'After addition = ' + add(arr);
   });
</script>
</body>
</html>

輸出

以上程式碼將產生以下輸出 −

單擊“點選此處”按鈕 −

更新於: 16-Jul-2020

874 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告