【发布时间】:2011-09-03 07:19:26
【问题描述】:
我有两个函数,一个函数调用另一个函数。
下面是我创建的代码结构的一个简单类比:
function test1(){
alert(a);//a is not identified here even when test1() is called under test2 which has a defined in scope
alert(b);//b is identified here
}
function test2(){
var a = 'a';//Defining a
test1();//Calling test1()
}
var b = 'b';
test2();
根据上面的代码,函数 test1() 能够识别变量 b 但不能识别变量 a。
我的问题是为什么变量 a 不在 test1 函数的范围内,即使我们在定义变量 a 的 test2() 中调用 test1() ?
提前致谢。
【问题讨论】:
标签: javascript function scope