如何在 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 {
      font-weight: 500;
      font-size: 20px;
      color: blueviolet;
   }
</style>
</head>
<body>
<h1>Pass arguments to anonymous functions</h1>
<div class="result"></div>
<br />
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to call an anonymous function and pass
parameters to it</h3>
<script>
   let BtnEle = document.querySelector(".Btn");
   let resEle = document.querySelector(".result");
   let add = function (a, b) {
      return a + b;
   };
   BtnEle.addEventListener("click", (event) => {
      resEle.innerHTML = "The sum of 22 and 19 = " + add(22, 19);
   });
</script>
</body>
</html>

输出

单击“此处单击”按钮后 -

更新于:2020 年 7 月 18 日

867 次浏览

开始你的职业

通过完成课程获得认证

立即开始
广告
© . All rights reserved.