1. 程式人生 > >JavaScript-4.2函式,變數作用域---ShinePans

JavaScript-4.2函式,變數作用域---ShinePans

<html>
	<head>
		<meta http-equiv="content-type" content="text/html;charset=GB2312"/>
		<title> 4.2 函式和變數作用域 </title>
		<!--指令碼部分-->
		<script type="text/javascript">
			var v1,v2;
			
			v1=10;
			v2=20;
			
			function a(){
				var v2,v3;
				alert("v1="+v1+"\r\nv2= "+v2+"\r\nv3="+v3);
				v2=v3=40;
				
				function b(v3,v4){
					alert("v3 = "+v3+"\r\nv4="+v4);
					v2=v3=80;
					}
					b(v3);
					alert("v2="+v2+"\r\nv3="+v3);
					alert(v4);
				}
				a();
				</script>
			</head>
			<body style="overflow:auto;">
			</body>
		</html>


函式外的變數在函式裡面無需宣告直接使用,函式裡面宣告的變數在函式外部是無效的.