본문 바로가기
JSP/이론

람다식

by SEOKIHOUSE 2023. 7. 14.
  • 그럼 람다가 뭔데..

람다 vs 평소 쓰던방식

람다vs 평소방식


  • 매개변수가 1개 일 경우 ( )생략가능    or 코드가 1줄일 경우 { }를 생략 할 수 있다

코드가 1줄 생략가능


매개변수 2개 코드 2줄이라 () {} 생략안됨


매개변수 2개라 ()생략불가 코드1줄이라 {}생략가능

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn" onclick="f2()">run1</button>
    <hr>
    <p id="demo"></p>

    <script>
        const btn = document.querySelector("#btn");
        btn.addEventListener("click", f);

        function f() {
            fetch('https://jsonplaceholder.typicode.com/users')
                .then(resp => resp.text())
                .then(value1 => document.getElementById("demo").innerHTML = value1)
        }

        function f2() {
            fetch('https://jsonplaceholder.typicode.com/users')
                .then(resp => {
                    return resp.text()
                })
                .then(function(value1) {
                    document.getElementById("demo").innerHTML = value1;
                })
        }

        let f3 = function(a,b) {
            let c = a+b;
            return c;
        }

        let f4 = (a,b) =>{
            let c = a+b;
            return c;
        }
        let f5 = (a,b) => a+b;

        
    </script>
</body>

</html>