在函数内部创建的变量不能在函数外部访问。

例如在:

function haha()
{
   var a = 5;
}


在haha()外部无法访问a。

这就是关闭方便的地方:

function haha()
{
    var a = 5;
    return function hahainside(){ }
}
newfunction = hahainside();


现在,如果我调用newfunction(),它可以访问haha()中的“ a”变量。

我对么?

我也想知道为什么:

<script>
    function exclusive()
    {
         d = "Robert, Ryo, Yuri";
         function returnfunction() { alert( d ); }
         return returnfunction();
    }
    ryo = exclusive();
    alert(7);
</script>


我也只是想知道为什么alert(7);不显示在我的浏览器上。

最佳答案

这是您需要知道的:

var a = 0;
function haha(){
  a = 5; // when calling haha `a` is accessible to everything below where var is declared
  return function(){
    a = 10; // same as notes above
  }
}
var newfunc = haha(); // `a` will become 5 where var is declared
newfunc(); // now `a` is 10 where var is declared
console.log(a); // works


无需在var a之外声明haha

function haha(){
  var a = 5; // only available in `haha`
  return function(){
    a = 10; // nearly useless - only scoped to `haha`
  }
}
var newfunc = haha(); // `a` will become 5 where var is declared
newfunc(); // now `a` is 10 where var is declared - can't access `a` where `newfunc` is called
console.log(a); // undefined


也许您想要一个构造函数:

function haha(arg){
  var whatever = 'something'; // never accessible in a new instance from outside
  this.a = arg;
  this.hahaInside = function(val){
    this.a = val;
  }
}
var newfunc = new haha(20); // `newfunc.a` is now `20`
newfunc.a = 12; // `newfunc.a` is now `12`
newfunc.hahaInside(10); // newfunc.a is now 10


作为对象:

var haha = {
  a: 5,
  hahaInside: function(val){
    this.a = val;
  }
}


对象中没有私有变量,也没有构造函数参数。您不必将新实例称为对象,但是您可能需要一个新实例。为避免编写关键字this

if(!Object.create){
  Object.create = function(o){
    function F(){}
    F.prototype = o;
    return new F;
  }
}
var newfunc = Object.create(haha); // new 'newfunc` is a new instance of `haha`


实际使用,无封闭:

// assuming 3 radio buttons
var ary = ['a', 'b', 'c'], radioButtons = document.getElementsByName('radButs');
var resultVar = '';
for(var i=0,l=radioButtons.length; i<l; i++){
    radioButtons[i].onclick = function(){
      resultVar = ary[i];
  }
}
// resultVar will always be `'c'` because by the time the click Event is called the loop has run all the through and the scope of `i` is global


闭包可防止变量的作用域更高。在这种情况下为全局:

// assuming 3 radio buttons
var ary = ['a', 'b', 'c'], radioButtons = document.getElementsByName('radButs');
var resultVar = '';
for(var i=0,l=radioButtons.length; i<l; i++){
  (function(i){
    radioButtons[i].onclick = function(){
      resultVar = ary[i];
    }
  })(i);
}
// now `resultVar` will be `'a'`, `'b'`, or `'c'` respectively, depending on the radio button that you clicked because  `i` becomes separately scoped at each step of the loop even by the time the click Event is called due to user action


您还可以使用闭包来创建伪静态函数,如下所示:

var count = (function(){
  var c = 0;
  return function(){
    return c++;
  }
})();


现在,每次您调用count()时,它都会这样做。之所以起作用,是因为该自执行函数会立即被调用,在其中定义var c并返回一个未执行的函数,该函数会增加c的作用域。

09-19 17:45