JavaScript 1.2에서는 함수 정의를 다른 함수 내에서도 중첩할 수 있습니다. 그러나 함수 정의가 루프나 조건문 내에 나타나지 않을 수 있다는 제한이 있습니다. 함수 정의에 대한 이러한 제한은 함수 문이 있는 함수 선언에만 적용됩니다.
예시
다음 예제를 실행하여 중첩 함수를 구현하는 방법을 배울 수 있습니다.
라이브 데모
<html>
<head>
<script>
<!--
functionhypotenuse(a,b) {
functionsquare(x){returnx*x;}
returnMath.sqrt(square(a)+square(b));
}
functionsecondFunction() {
varresult;
result=hypotenuse(1,2);
document.write(result );
}
/-->
</script>
</head>
<body>
<p>Clickthe following button to call the function</p>
<form>
<inputtype="button"onclick="secondFunction()"value="CallFunction">
</form>
</body>
</html>