什么是JavaScript嵌套函数?
JavaScript1.2允许函数定义也嵌套在其他函数中。仍然有一个限制,即函数定义不能出现在循环或条件中。这些对函数定义的限制仅适用于带有函数语句的函数声明。
示例
您可以尝试运行以下代码以了解如何实现JavaScript嵌套函数-
<html>
<head>
<script>
function hypotenuse(a, b) {
function square(x) { return x*x; }
return Math.sqrt(square(a) + square(b));
}
function secondFunction(){
var result;
result = hypotenuse(3,4);
document.write ( result );
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "secondFunction()" value = "Find Hypotenuse">
</form>
</body>
</html>热门推荐
10 新年祝福语简短的个性
11 项目总生日祝福语简短
12 歌词祝福语高级文案简短
13 老师新婚快乐祝福语简短
14 宝宝新年祝福语大全简短
15 六一加端午祝福语简短
16 生日祝福语舅舅 简短独特
17 预祝高尔夫的祝福语简短
18 元旦祝福语简短有深度