JavaScript Nested Functions函数详解

首页 / JavaScript入门教程 / JavaScript Nested Functions函数详解

在JavaScript 1.2之前,仅在Top Level Global代码中允许使用函数定义,但是JavaScript 1.2允许将函数定义也嵌套在其他函数中。仍然有一个限制,即函数定义不能出现在循环或条件中,这些对函数定义的限制仅适用于带有函数语句的函数声明。

请尝试运行以下示例,以了解如何实现嵌套函数。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/javascript/javascript-nested-functions.html

来源:LearnFk无涯教程网

<html>
   <head>
      <script type="text/javascript">
         <!--
            function hypotenuse(a, b) {
               function square(x) { return x*x; }
               return Math.sqrt(square(a) + square(b));
            }
            function secondFunction() {
               var result;
               result=hypotenuse(1,2);
               document.write ( result );
            }
         //-->
      </script>
   </head>
   
   <body>
      <p>Click the following button to call the function</p>
      
      <form>
         <input type="button" onclick="secondFunction()" value="Call Function">
      </form>
      
      <p>Use different parameters inside the function and then try...</p>
   </body>
</html>

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Vue开发实战 -〔唐金州〕

编辑训练营 -〔总编室〕

TypeScript开发实战 -〔梁宵〕

深入浅出云计算 -〔何恺铎〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

操作系统实战45讲 -〔彭东〕

朱涛 · Kotlin编程第一课 -〔朱涛〕

商业思维案例笔记 -〔曹雄峰〕

零基础GPT应用入门课 -〔林健(键盘)〕

好记忆不如烂笔头。留下您的足迹吧 :)