【发布时间】:2010-08-13 06:42:18
【问题描述】:
我从 Google Code Playground http://code.google.com/apis/ajax/playground/ 获取了这个
/*CLOSURE
* When a function is defined in another function and it
* has access to the outer function's context even after
* the outer function returns
* An important concept to learn in Javascript
*/
function outerFunction(someNum) {
var someString = 'Hai!';
var content = document.getElementById('content');
function innerFunction() {
content.innerHTML = someNum + ': ' + someString;
content = null; // IE memory leak for DOM reference
}
innerFunction();
}
outerFunction(1);
///////////////////////
没关系,但是如果我在内部函数中有一个与外部函数中的变量同名的局部变量,那么如何访问该变量?
function outerFunction(someNum) {
var someString = 'Hai!';
var content = document.getElementById('content');
function innerFunction() {
var someString='Hello';
content.innerHTML = someNum + ': ' + someString;
content = null; // IE memory leak for DOM reference
}
innerFunction();
}
outerFunction(1);
【问题讨论】:
-
我插入了一行 var ss=someString;内部函数内部。然后当我尝试访问 ss 它返回 undefined
标签: javascript