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>