Lambda 함수는 하나의 표현식으로 구성되고 하나 또는 여러 개의 매개변수를 취할 수 있는 작은 익명 함수입니다. 기본적으로 함수가 다른 함수에 매개변수로 전달되도록 허용합니다. 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;
}
.sample,
.result {
font-size: 20px;
font-weight: 500;
color: blueviolet;
}
.sample {
color: red;
}
</style>
</head>
<body>
<h1>Lambdas with Arrow Functions in JavaScript</h1>
<div class="sample">[1,2,3,4,5,6,7]</div>
<div class="result"></div>
<br />
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to square the array above</h3>
<script>
let BtnEle = document.querySelector(".Btn");
let sampleEle = document.querySelector(".sample");
let resEle = document.querySelector(".result");
let arr = [1, 2, 3, 4, 5, 6, 7];
let square = (item) => item * item;
function arraySq(func, arr) {
let newArr = [];
arr.forEach((element) => {
newArr.push(func(element));
});
resEle.innerHTML = "The new array = " + newArr;
}
BtnEle.addEventListener("click", (event) => {
arraySq(square, arr);
});
</script>
</body>
</html> 출력

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