본문 바로가기

Programming

[Javascript] function ()() 문법 해석

* 함수명()() 구조에 대해 분석

 

1. 다음과 같이 test라는 함수명에 결과를 m이라는 파라메터를 가지는 함수를 리턴한다.

 

function test(a,b) {
    return m => {
        switch(m) {
            case 1:
                return a + b;
            default:
                return 0;
        }
    }
}

 

2. 문법

const result = test(a, b)(m); // m이 1이면, a+b, 아니면 0 반환

 

3. 테스트

const result = test(1,2)(1);

console.log(result); // 3