与C/C++语言不同,在花括号中定义的变量,在花括号外同样有效。

 

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">
	function processClick()
	{
		{
			var v1 = "abc";
		}
		alert(v1);
	}
</script>

</head>
	
<body>

<div style="width:1920px;min-height:1080px" >
	<a href="#" onClick="processClick();">show</a>
</div>

</body>
</html>

 

但在函数内定义的变量,在函数的花括号外就无效了。

 

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">
	function processClick()
	{
		{
			var v1 = "abc";
		}
	}
	alert(v1);
</script>

</head>
	
<body>

<div style="width:1920px;min-height:1080px" >
	<a href="#" onClick="processClick();">show</a>
</div>

</body>
</html>