【发布时间】:2010-12-04 10:34:00
【问题描述】:
考虑以下代码:
<html><head></head>
<body>
<script type="text/javascript">
var outside_scope = "outside scope";
function f1() {
alert(outside_scope) ;
}
f1();
</script>
</body>
</html>
此代码的输出是警报框显示消息“外部 范围”。但是,如果我将代码稍微修改为:
<html><head></head>
<body>
<script type="text/javascript">
var outside_scope = "outside scope";
function f1() {
alert(outside_scope) ;
var outside_scope = "inside scope";
}
f1();
</script>
</body>
</html>
警报框显示消息“未定义”。我本可以有 如果在这两种情况下都显示“未定义”,则理解逻辑。但是,那个 没有发生。它仅在第二种情况下显示“未定义”。这是为什么呢?
提前感谢您的帮助!
【问题讨论】:
标签: javascript variables shadowing