<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>03localVarglobalVar.html</title>

    <script>

        var a = 1;//函數外宣告,是global變數

        function f() {

            var a = 2//函數內宣告,是local變數

            document.write("函數裡的var-a:" + a + "<br>")

        }

        document.write("沒有呼叫函數前呼叫a:" + a+"<br>")//1

        f();//2

        document.write("呼叫過f()再呼叫a:" + a + "<br><hr>")//1

 

 

 

        var b = 1;//函數外宣告,是global變數

        function g() {

            b = 2//沒有在函數內宣告,是global變數

            document.write("函數裡的b(未在函數內宣告var):" + b + "<br>")

        }

        document.write("沒有呼叫函數前呼叫b:" + b + "<br>")//1

        g();//2

        document.write("呼叫過g()再呼叫b:" + b + "<br><hr>")//2

 

 

 

        var c = 1;//函數外宣告,是glocal變數

        function h() {

            var c = 2//函數內宣告,是local變數

            c = 3//找最近的,所以是local變數

            document.write("函數裡的c(未在函數內宣告var):" + c + "<br>")

        }

        document.write("沒有呼叫函數前呼叫c:" + c + "<br>")//1

        h();//3

        document.write("呼叫過h()再呼叫c:" + c + "<br>")//1

 

 

 

    </script>

</head>

<body>

 

</body>

</html>

 

arrow
arrow
    全站熱搜

    乙方 發表在 痞客邦 留言(0) 人氣()