关于javascript的全部环境与局部环境的访问比较,一下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
var color="red";
function test(){
document.write("全局变量color颜色:"+color);
}
test(); //output: 全局变量color颜色:red
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
var color="red";
function test(){
var color="blue";
document.write("局部变量color颜色:"+color);
document.write("<br>");
}
test();
document.write("全局变量color颜色:"+window.color);
// output: 局部变量color颜色:blue
// 全局变量color颜色:red
</script>
</body>
</html>