Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

JavaScript의 뚱뚱한 대 간결한 화살표 함수

<시간/>

간결한 화살표는 단선 함수에 대한 뚱뚱한 화살표 함수의 유선형입니다. 함수 본문에 한 줄의 코드만 있으면 conscie 화살표 함수에 암시적 반환이 있으므로 함수 본문에 대해 중괄호 {}가 필요하지 않습니다. 또한 매개변수가 하나만 있으면 괄호() 없이 쓸 수 있지만 매개변수가 없으면 괄호가 필요합니다.

구문

뚱뚱한 화살표 기능 -

let add = (a,b) =>{return a+b;}

Consice 화살표 기능:

let add = (a,b)=>a+b;

매개변수가 하나만 있는 경우 -

let add = a=>a+22;

다음은 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-size: 20px;
      font-weight: 500;
      color: blueviolet;
   }
</style>
</head>
<body>
<h1>Fat vs concise arrow functions</h1>
<div class="result"></div>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to call the add() and multiply() arrow function</h3>
<script>
   let resEle = document.querySelector(".result");
   let add = (a, b) => a + b;
   let multiply = (a, b) => {
      return a * b;
   };
   document.querySelector(".Btn").addEventListener("click", () => {
      resEle.innerHTML = "Sum of 32 and 19 = " + add(32, 19) + "<br>";
      resEle.innerHTML =
      "Multiplication of 32 and 19 = " + multiply(32, 19) + "<br>";
   });
</script>
</body>
</html>

출력

JavaScript의 뚱뚱한 대 간결한 화살표 함수

'여기를 클릭' 버튼을 클릭하면 -

JavaScript의 뚱뚱한 대 간결한 화살표 함수