javascript
<!DOCTYPE html>
<html>
<head>
<title>变量作用域样例</title>
<script src="/lib/jquery/dist/jquery.js"></script>
</head>
<body>
<h1>变量作用域样例</h1>
<script>
</script>
<script>
var a = 1;//当前作用域的变量a
console.log("3: a的值是:" + a); //1
function test() {
var a = 3;//当前作用域是test函数体内,变量a重定义了,函数体里的a都是它
console.log("1:a的值是:", a); //3
a = 4;
console.log("2:a的值是:", a); //4
return a;
}
var b = test();//4
console.log("b的值是:" + b);
a = 10;//修改了全局的变量a
var c = test();//4
console.log("c的值是:" + c);
</script>
<script>
console.log("4: a的值是:" + a);//搜索同一级<script>标签里定义的变量a的值
</script>
</body>
</html>
