【发布时间】:2011-12-14 12:28:23
【问题描述】:
我正在尝试在 codeacademy 上学习 JS,但我无法理解/通过这件事。有人可以提供答案并解释为什么会这样吗?将深深感激。
// This function tries to set foo to be the
// value specified.
function setFoo(val) {
// foo is declared in a function. It is not
// accessible outside of the function.
var foo = val;
}
setFoo(10);
// Now that we are outside the function, foo is
// not defined and the program will crash! Fix this
// by moving the declaration of foo outside of the
// function. Make sure that setFoo will still update
// the value of foo.
alert(foo);
【问题讨论】:
-
你到底有什么不明白的?您是否尝试过将 foo 的声明移到函数之外?
标签: javascript variables scope