繁体   English   中英

如果存在具有相同名称的局部变量,如何访问闭包中的变量?

[英]how to access variables in closures if there are local variables with the same name?

我从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);

你不能,因为外部范围的变量内部函数的变量所遮蔽

innerFunction上的作用域链看起来像这样:

innerFunction                     outerFunction             global object
 ______________________         ________________________        _______________
|* someString = 'Hello'| <---- |  someString = 'Hai!'    | <---|* outerFunction|
 ----------------------        |* content = [HTMLElement]|     |    .....      |
                               |* someNum (argument)     |      ---------------
                               |* innerFunction          |
                                -------------------------

* Denotes a resolvable identifier from the scope of innerFunction.

每个函数都有自己的变量对象 ,其中函数声明,变量声明和函数形式参数的标识符作为属性存在。

这些对象不能通过代码直接访问,范围链由所有这些链接对象组成。

解析标识符后,查找在范围链中查找,查找它的第一次出现,直到到达全局对象,如果未找到标识符,则抛出ReferenceError

看看以下文章:

闭包的局部变量“影子”外部函数的同名变量,所以这样:

function outerFunction(s) {
  var someString = 'Hai!';
  function innerFunction() {
    var someString='Hello';
    alert(someString);
  }
  innerFunction();
}
outerFunction();

会提醒Hello

解决它的唯一方法是重命名变量,或者传入你想要使用的变量:

function outerFunction(s) {
  var someString = 'Hai!';
  function innerFunction(outer) {
    var someString='Hello';
    alert(outer);
  }
  innerFunction(someString);
}
outerFunction();        

请警惕Hai!

要了解范围链, 请查看以前的范围问题。

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);

尝试使用this.varname来引用闭包中的那个。

你的例子中有this.someString。

你可以

  • 只需避免创建具有相同名称的任何局部变量,或
  • 声明同名的局部变量 之前 ,创建该闭包变量的本地副本(如果你真的想要一个同名的局部变量)

(有一个解决方法,但在指出它实际上不起作用后删除它)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM