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>