第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

無(wú)涯教程-Javascript - 函數(shù)聲明

標(biāo)簽:
JavaScript

函数是一组可重用的代码,可以在程序中的任何位置调用,这样就无需一次又一次编写相同的代码,它可以帮助程序员编写模块化代码。

JavaScript允许无涯教程编写自己的函数,本节说明如何使用JavaScript编写自己的函数。

函数定义

在使用函数之前,需要对其进行定义。在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>

函数参数

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

请尝试以下示例,在这里修改了 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>

Return 语句

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>

关于JavaScript函数,有很多知识要学习,但是已经在本教程中介绍了最重要的概念。

  • JavaScript nested Function  (嵌套函数)

  • JavaScript Function Constructors  (构造函数)

  • JavaScript Function Literals   (函数字面量)

参考链接

https://www.learnfk.com/javascript/javascript-functions.html

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消