상세 컨텐츠

본문 제목

함수 arguments 말고 rest 파라미터

Javascript

by 모모87 2021. 8. 25. 17:38

본문

 

function 함수2(...파라미터들) {
            console.log(파라미터들);
        }
        함수2(1, 2, 3, 4, 5);

        function 함수3(a, b, ...파라미터들) {
            console.log(파라미터들);
        }
        함수3(1, 2, 3, 4, 5);

        function 함수4(a, b, ...파라미터들) {
            console.log(파라미터들[2]);
        }
        함수4(1, 2, 3, 4, 5);
        //5

        function 함수5(...rest) {
            for (var i = 0; i < rest.length; i++) {
                console.log(rest[i]);
            }
        }
        함수5(1, 2, 3, 4, 5);
        //1~5출력

        function 함수6(a, ...rest) {
            for (var i = 0; i < rest.length; i++) {
                console.log(rest[i]);
            }
        }
        함수6('시작', '시작2', 3, 4, 5, 34, 34, 234, 123, 41, 235, 123, 5,);

        // ...rest 파라미터는 주의점 
        // 1.가장 뒤에 써야함
        // 2.2번이상 금지

 

반응형

관련글 더보기