標籤

2021年8月18日 星期三

JavaScript程式碼使用寫法

 

函式

建立函式

宣告函式

    function realcool()

宣告函式內的程式碼

    function reallycool()
    {
        JavaScript程式碼
    }

函式命名

  • 名稱大小寫有別
  • 使用合法字元及避免使用保留字
  • 取個有意義的名稱
        function print_bold_text()
        {
            document.write("This is a bold statement!");
        }
    

函式的參數

    function reallycool(coolcar, coolplace)
    {
        JavaScript程式碼
    }

函式參數的值

    function reallycool(coolcar, coolplace)
    {
        var mycar = coolcar;
    }
    function reallycool(coolcar, coolplace)
    {
        document.write("My car is a " + coolcar);
    }

使用多個參數

    function reallycool(coolcar, coolplace)
    {
        document.write("My car is a " + coolcar + " and I drive it to " + coolplace);
    }

函式內的 return 敘述

    function hello(name)
    {
        return "Hello, Mr. or Ms. " + name;
    }

在 script 中呼叫函式

    hello("Lee");

在 HEAD 區域呼叫函式

建立 JavaScript 訊息方塊
    window.alert("This is an alert!");
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
        window.alert("This is an alert!");
//-->
</SCRIPT>
</HEAD>
<BODY>
        HTML 碼
</BODY>
</HTML>
</PRE>

在函式中呼叫 JavaScript 訊息方塊
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
    function show_message()
    {
        window.alert("This is an alert!");
    }
    show_message();
//-->
</SCRIPT>
</HEAD>
<BODY>
        HTML 碼
</BODY>
</HTML>
</PRE>

在 BODY 區域呼叫函式

<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
    function print_text()
    {
        document.write("I came from a function!");
    }
//-->
</SCRIPT>
</HEAD>
<BODY>
<H4>在 BODY 區域呼叫函式</H4>
<SCRIPT language="JavaScript">
<!--
    print_text();
//-->
</SCRIPT>
<p>
<B>This is a bold statement!</B>
</BODY>
</HTML>

從一個函式呼叫另一個函式

<HEAD>
<SCRIPT language="JavaScript">
<!--
    function update_alert()
    {
        window.alert("Welcome! This site is updated daily!");
    }
    function call_alert()
    {
        update_alert();
    }
    call_alert();
//-->
</SCRIPT>
</HEAD>
另一例子:
<HEAD>
<SCRIPT language="JavaScript">
<!--
    function update_alert()
    {
        window.alert("Welcome! This site is updated daily!");
    }
    function section_alert()
    {
        window.alert("Please visit the picture section!");
    }
    function links_alert()
    {
        window.alert("Also, check out my links page!");
    }
    function get_message()
    {
        update_alert();
        section_alert();
        links_alert();
    }
    get_message();
//-->
</SCRIPT>
</HEAD>

呼叫有參數的函式

<HEAD>
<SCRIPT language="JavaScript">
<!--
    function alerts( message )
    {
        window.alert( message );
    }
    function get_message()
    {
        alerts("Welcome! This site is updated daily!");
        alerts("Please visit the picture section!");
        alerts("Also, check out my links page!");
    }
    get_message();
//-->
</SCRIPT>
</HEAD>
使用全域變數
<SCRIPT language="JavaScript">
<!--
    var mycar = "Honda";
    var paycheck = 1200;
    function new_car()
    {
        mycar = "Ferrari";          // 全域變數
        paycheck = 3500;            // 全域變數
        window.alert( "You need $" + paycheck + " to get a " + mycar );
    }
    new_car();
    window.alert( "You make $" + paycheck + " and have a " + mycar );
//-->
</SCRIPT>
使用區域變數
<SCRIPT language="JavaScript">
<!--
    var mycar = "Honda";
    var paycheck = 1200;
    function new_car()
    {
        var mycar = "Ferrari";      // 區域變數
        var paycheck = 3500;        // 區域變數
        window.alert( "You need $" + paycheck + " to get a " + mycar );
    }
    new_car();
    window.alert( "You make $" + paycheck + " and have a " + mycar );
//-->
</SCRIPT>

呼叫有 return 敘述的函式

<SCRIPT language="JavaScript">
<!--
    function get_added_text()
    {
        var text1 = "This is ";
        var text2 = "fun!";
        var added_text = text1 + text2;
        return added_text;
    }
    var alert_text = get_added_text();
    window.alert( alert_text );
//-->
</SCRIPT>
來源出處: https://www.csie.ntu.edu.tw/~sylee/courses/jscript/function.htm

使用 DiskSpd 測試磁碟效能

  DiskSpd 是微軟創建的命令行磁碟測試工具。它結合了強大的IO工作負載定義來測量磁碟效能。由於它支援自由配置和調整參數,使其成為存儲效能測試、驗證和基準測試的理想工具。 步驟 1. 從 GitHub (說明) https://github.com/Microsoft/di...