js---局部变量和全局变量(3)---实例

对于没有声名的变量:都作为windows的属性

<script>

var x = "aaa";

function square ()

{
               x = "ddd";

               alert(x);//x value "ddd"

              var x = "fff";

              alert(x);//x value "fff"
}

square();

alert(x);//x value "aaa"

</script>

 

<script>
var msg="全局变量";

function show()
{
    msg="局部变量";
}

show();

alert(msg);//msg value "局部变量"
</script>

 

<script>
var msg="全局变量";

function show()
{
    var msg="局部变量";
}

show();

alert(msg);//msg value "全局变量"
</script>

 

<script>

var x=111;
function show()
{
  var typeValue=typeof(x);

  alert(typeValue);
  alert(typeof(x)== "undefined");
}

show();
</script>

 

<script>

var x=111;

function square ()

{
               x = "ddd";

              var x="fff";
}

square();

alert(typeof(x));// alert value number
alert(x);// alert x value 111

</script>

 

 

<script>

var x=111;

function square ()

{
               x = "ddd";
}

square();

alert(typeof(x));// alert value string
alert(x);// alert x value "ddd"

</script>

你可能感兴趣的:(js---局部变量和全局变量(3)---实例)