<html>
<head>
<script type="text/javascript">
     var z=1;
    function abc(){
	    x =123;
		var y=12;
	}
	abc();
	alert(x);
	alert(y);
</script>
</head>
<body>
   
</body>  
</html>

 可以正常打印出x的值;x为全局变量;

 

<html>
<head>
<script type="text/javascript">
     var z=1;
    function abc(){
	    x =123;
		var y=12;
	}
	//abc();
	alert(x);
	alert(y);
</script>
</head>
<body>
   
</body>  
</html>

 将abc();注释后由于没有调用函数,会导致x出现未定义的现象。在函数中的没有定义变量只有当函数被调用的时候才能进行变量的创建。

相关文章:

  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2021-05-14
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案