当前位置: 首页 » 综合知识 » it知识 » 正文

JavaScript函数如何定义

发布时间:2023-07-10 以下文章来源于网友投稿,内容仅供参考!

功能定义

在我们使用一个函数之前,我们需要定义它。在 JavaScript 中定义函数的最常见方法是使用function关键字,后跟唯一的函数名称、参数列表(可能为空)和用大括号括起来的语句块。

句法

此处显示了基本语法。

<script type = "text/javascript">
   <!--
      function functionname(parameter-list) {
         statements
      }
   //--></script>

例子

试试下面的例子。它定义了一个名为 sayHello 的函数,它不接受任何参数

<script type = "text/javascript">
   <!--
      function sayHello() {
         alert("Hello there");
      }
   //--></script>

调用函数

要在脚本稍后的某处调用函数,您只需编写该函数的名称,如以下代码所示。

<html>
   <head>   
      <script type = "text/javascript">
         function sayHello() {            document.write ("Hello there!");
         }      </script>      
   </head>   
   <body>
      <p>Click the following button to call the function</p>      
      <form>
         <input type = "button" onclick = "sayHello()" value = "Say Hello">
      </form>      
      <p>Use different text in write method and then try...</p>
   </body></html>

输出

Click the following button to call the functionUse different text in write method and then try...

功能参数

到目前为止,我们已经看到了没有参数的函数。但是有一个工具可以在调用函数时传递不同的参数。这些传递的参数可以在函数内部捕获,并且可以对这些参数进行任何操作。一个函数可以接受多个用逗号分隔的参数。

例子

试试下面的例子。我们在这里修改了sayHello函数。现在它需要两个参数。

<html>
   <head>   
      <script type = "text/javascript">
         function sayHello(name, age) {            document.write (name + " is " + age + " years old.");
         }      </script>      
   </head>   
   <body>
      <p>Click the following button to call the function</p>      
      <form>
         <input type = "button" onclick = "sayHello('Zara', 7)" value = "Say Hello">
      </form>      
      <p>Use different parameters inside the function and then try...</p>
   </body></html>

输出

Click the following button to call the functionUse different parameters inside the function and then try...

声明

JavaScript 函数可以有一个可选的return语句。如果要从函数返回值,这是必需的。该语句应该是函数中的最后一个语句。

例如,您可以在一个函数中传递两个数字,然后您可以期望该函数在您的调用程序中返回它们的乘法。

例子

试试下面的例子。它定义了一个函数,该函数接受两个参数并将它们连接起来,然后在调用程序中返回结果。

<html>
   <head>  
      <script type = "text/javascript">
         function concatenate(first, last) {            var full;
            full = first + last;            return full;
         }         function secondFunction() {            var result;
            result = concatenate('Zara', 'Ali');            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>

输出

Click the following button to call the functionUse different parameters inside the function and then try...
  • • Linux Ecdsa密钥长度选择有何依据

    在Linux

  • • Linux Khook在内核监控中的应用如何

    Linux

  • • Linux Gsoap是否支持异步通信

    GSOAP是

  • • Linux Coremail如何提升用户体验

    提升Linu

  • • Linux Ecdsa算法有哪些局限性

    ECDSA

  • 哎呀音乐钢琴键盘学习《 钢琴主人训练营》 西瓜学琴
    郭蝈 陪练钢琴 30节课时 考级刚需 让孩子每一次练琴都是高质量的
    30天轻松学会五线谱 流行钢琴自学初级教程 牙牙学琴
    流行爵士钢琴实战技巧VIP课 - 继伟 哎呀音乐
    【海上钢琴师】原版 MT1990钢琴谱
    百首经典流行钢琴实战曲集 - 继伟
    雷费尔德电钢琴重锤88键专业考级儿童初学者数码电子钢琴家用
    小练咖 真人钢琴陪练 1v1服务 2999随时退 1课时50分钟 考级刚需
    雅马哈电钢琴88键重锤CLP735智能数码电子钢琴家用专业初学者考级
    【原装进口】Yamaha/雅马哈钢琴 b121 SC2原声静音钢琴
  • 珠海专业调钢琴
  • 天津宝坻区调钢琴
  • 天津静海区钢琴调音
  • 成都简阳市钢琴调律
  • 大连瓦房店市钢琴调音
  • 眉山调钢琴联系方式
  • 惠州大亚湾钢琴调琴师
  • 长治调琴师
  • 厦门湖里区钢琴调音师
  • 上海普陀区钢琴调音师