0

刚开始学习闭包,在 FireBug 中写了这段代码:

var later;

function outerFunc() {
  var innerVar = "Inside Outer";
  function innerFunc() {
     console.log(innerVar);
  }

  later = innerFunc;
};

outerFunc();
later();

但是在调用later();它时会返回一个未定义的值。不应该打印出来吗"Inside Outer" ?

4

1 回答 1

1
var later;

function outerFunc() {
  var innerVar = "Inside Outer";
  function innerFunc() {
     console.log(innerVar);
  }

  later = innerFunc;
};

outerFunc(); //Don't run innerFunc, just set later = innerFunc
later(); // Execute innerFunc

所以它只记录一次..看起来不错..

于 2013-04-08T18:30:19.763 回答