js-作用域演示
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>js-作用域演示</title>
</head>
<body>
<script>
//全局作用域
var a="123";
function test(){
console.log("test内调用外部变量a:"+a); //可以作用到任何地方
}
test();
//函数作用域
function test(){
function inner(){
console.log("inner");
}
inner();
}
test();
inner();
//函数内变量作用域
function test(){
var b="123";
console.log("test内部变量b:"+b);
}
test();
console.log("外部调用变量b:"+b);
//函数内变量作用域2
function test(){
b="123";
console.log("test内部变量b:"+b);
}
test();
console.log("外部调用变量b:"+b);
//块级作用域,JS没有块级作用域
for(var b=1;b<5;b++){
}
console.log(b);
//预编译变量和函数
var c="123";
function test(){
//console.log(d);
console.log(c);
var c="456";
console.log(c);
}
test();
</script>
</body>
</html>