기본값
이것은 함수 매개변수를 쉽게 처리하기 위해 왔습니다. 기본 매개변수를 쉽게 설정하여 형식 매개변수를 기본값으로 초기화할 수 있습니다. 값이 없거나 정의되지 않은 경우에만 가능합니다. 예를 들어보겠습니다.
예시
라이브 데모
<html>
<body>
<script>
// default is set to 1
function inc(val1, inc = 1) {
return val1 + inc;
}
document.write(inc(10,10));
document.write("<br>");
document.write(inc(10));
</script>
</body>
</html> 휴식
ES6는 개발자의 작업을 쉽게 하기 위해 rest 매개변수를 가져왔습니다. 인수 객체의 경우 나머지 매개변수는 세 개의 점으로 표시되고 ... 매개변수 앞에 옵니다.
예시
다음 코드 스니펫을 살펴보겠습니다 −
<html>
<body>
<script>
function addition(…numbers) {
var res = 0;
numbers.forEach(function (number) {
res += number;
});
return res;
}
document.write(addition(3));
document.write(addition(5,6,7,8,9));
</script>
</body>
</html> 구조화
패턴 일치로 바인딩하기 위해 ES6에 도입된 매개변수입니다. 값을 찾지 못하면 undefined를 반환합니다. ES6에서 배열을 개별 변수로 파괴하는 방법을 살펴보겠습니다.
예시
라이브 데모
<html>
<body>
<script>
let marks = [92, 95, 85];
let [val1, val2, val3] = marks;
document.write("Value 1: "+val1);
document.write("<br>Value 2: "+val2);
document.write("<br>Value 3: "+val3);
</script>
</body>
</html>