<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>index15-6</title>
    <script type="text/javascript">
        
        var Msg = "Hello,This a outside of Func1().";
        alert(Msg);     //呼叫全域變數Msg時,已經被宣告為outside
        Func1();         //函式內呼叫Msg時,因為函式內沒有自行宣告一個Msg變數,程式會自行往外找,並賦予新的值
        alert(Msg);    //全域變數Msg因在函式內被賦予新的inside值,所以這裡呼叫的會是inside值

        function Func1(){

            Msg= "Hello,This is a inside of Func1()."
            alert(Msg);
        }
    </script>
</head>
<body>

</body>
</html>

 

如果函式內使用到函式外相同的變數,也沒有另外宣告,那麼此全域變數的值會被函式內的設定覆蓋。

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 A&B 的頭像
    A&B

    網頁前端新手上路辛酸史

    A&B 發表在 痞客邦 留言(0) 人氣()