admin管理员组文章数量:1289394
I took this from Google Code 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);
///////////////////////
Its all ok, but if I have a local variable in the inner function with the same name as a variable in the outer function then how to access that variable?
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);
I took this from Google Code Playground http://code.google./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);
///////////////////////
Its all ok, but if I have a local variable in the inner function with the same name as a variable in the outer function then how to access that variable?
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);
Share
Improve this question
asked Aug 13, 2010 at 6:42
SriramSriram
1,1902 gold badges15 silver badges27 bronze badges
2
- I inserted a line var ss=someString; inside the innerfunction. Then when I tried to access ss it returned undefined – Sriram Commented Aug 13, 2010 at 6:48
- Very similar / duplicate? question: stackoverflow./questions/1484143/scope-chain-in-javascript – Peter Ajtai Commented Aug 13, 2010 at 6:59
5 Answers
Reset to default 8You can't, because the variable of the outer scope is shadowed by the one on your inner function.
The scope chain on the innerFunction
looks something like this:
innerFunction outerFunction global object ______________________ ________________________ _______________ |* someString = 'Hello'| <---- | someString = 'Hai!' | <---|* outerFunction| ---------------------- |* content = [HTMLElement]| | ..... | |* someNum (argument) | --------------- |* innerFunction | ------------------------- * Denotes a resolvable identifier from the scope of innerFunction.
Each function has its own Variable Object, is where the identifiers of Function Declarations, Variable Declarations, and function Formal Parameters live, as properties.
Those objects are not directly accessible by code, the scope chain is formed by all those chained objects.
When an identifier is resolved, the lookup goes up in the scope chain, looking for the first appearance of it, until the global object is reached, if the identifier is not found, a ReferenceError
is thrown.
Give a look to the following articles:
- ECMA262-3 in detail: The Variable Object
- Variables vs. Properties in JavaScript
The local variables of the closure "shadow" the variables of the same name from the outer function, so this:
function outerFunction(s) {
var someString = 'Hai!';
function innerFunction() {
var someString='Hello';
alert(someString);
}
innerFunction();
}
outerFunction();
will alert Hello
.
The only way to work around it is to rename your variables, or pass in the variable you want to work with:
function outerFunction(s) {
var someString = 'Hai!';
function innerFunction(outer) {
var someString='Hello';
alert(outer);
}
innerFunction(someString);
}
outerFunction();
Will alert Hai!
To learn about the scope chain, take a look at this previous scope question.
Try with this.varname to refer to the one in the closure.
this.someString in your example.
You could
- simply avoid creating any local variable with the same name, or
make a local copy of that closure variable before declaring the local variable of the same name (if you really want a local variable of the same name)
(had a workaround but removed it after it was pointed out that it wouldn't actually work)
function outerFunction(someNum) {
var someString = 'Hai!';
var content = document.getElementById('content');
function innerFunction() {
this.someString = 'Hello';
content.innerHTML = someNum + ': ' + someString;
content = null; // IE memory leak for DOM reference
}
innerFunction();
} outerFunction(1);
本文标签:
版权声明:本文标题:javascript - how to access variables in closures if there are local variables with the same name? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741453797a2379639.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论