如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?

我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。


当前回答

我写了一篇博客帖子一段时间后解释关闭,这里是我在关闭方面所说的,为什么你会想要一个。

关闭是让一个函数有持久的,私人变量的方式 - 也就是说,只有一个函数知道的变量,在那里它可以跟踪从以前运行的时间的信息。

在这个意义上,他们让一个函数有点像一个具有私人属性的对象。

完整帖子:

那么,这些关闭事物是什么呢?

其他回答

最好的方法是不断地解释这些概念:

console.log(x);
// undefined

var x = 42;
console.log(x);
// 42

现在,JavaScript知道X是什么意思。

x = 43;
console.log(x);
// 43

现在,X意味着别的东西。

范围

当您创建一个函数时,该函数为变量具有自己的“盒子”。

function A() {
  var x = 42;
}

console.log(x);

// undefined

var x = 42;

function A() {
  console.log(x);
}

// 42

在函数 A 中,您有“范围访问”到 x。

function A() {
  var x = 42;
}

function B() {
  console.log(x);
}

// undefined

function A() {

  var x = 42;

  function B() {
    console.log(x);
  }

}

// 42

功能

在JavaScript中,您通过称之为:

function A() {
  console.log(42);
}

A();

// 42

var a = function() {
  console.log(42);
};

变量现在意味着一个功能,你可以运行它。

a();
// 42

setTimeout(a, 1000);

在一秒钟(1000毫秒),函数一个点被称为:

// 42

现在,当你定义函数时,这些函数可以访问它们的外部目标。

var a = function() {

  var text = 'Hello!'

  var b = function() {
    console.log(text);
    // inside function `b`, you have access to `text`
  };

  // but you want to run `b` later, rather than right away
  setTimeout(b, 1000);

}

现在发生了什么?

// 'Hello!'

var c;

var a = function() {

  var text = 'Hello!'

  var b = function() {
    console.log(text);
    // inside function `b`, you have access to `text`
  };

  c = b;

}

// now we are out side of function `a`
// call `a` so the code inside `a` runs
a(); 

// now `c` has a value that is a function
// because what happened when `a` ran

// when you run `c`
c();

// 'Hello!'

您仍然可以在关闭范围内访问变量。

即使一个已经完成了运行,现在你正在运行C的外部。

在这里发生的事情被称为JavaScript中的“关闭”。

关闭基本上是创造两件事:一个功能,一个私人范围,只有这个功能才能访问。

它就像把一些涂层围绕着一个功能一样。

因此,对于一个6岁的,它可以通过给出一个模拟来解释。 让我们说我建造一个机器人. 这个机器人可以做很多事情. 在这些事情中,我编程它来计算他在天空中看到的鸟类数量. 每当他看到25只鸟,他应该告诉我他从一开始就看到了多少只鸟。

我不知道他看见了多少鸟,除非他告诉我,只有他知道,这就是私人范围,这基本上是机器人的记忆,让我们说我给了他4GB。

告诉我他看到的几只鸟是返回的功能,我也创造了它。

这个模拟有点可怕,但有人可以改进我猜。

有些人非常接近得到简单的解释,然后变得复杂或保持抽象,击败目的,并无法显示一个非常简单的现实世界使用。

关闭有访问包含函数的变量 关闭在自己的内存空间中持久(因此对所有类型的 oop-y 实例物品都是有用的)

function sayHello(name) {
  var text = 'Hello ' + name; // Local variable
  console.log(text);
  var sayAlert = function () {
      alert(text);
  }
  return sayAlert;
}

sayHello(); 
/* This will write 'Hello undefined' to the console (in Chrome anyway), 
but will not alert though since it returns a function handle to nothing). 
Since no handle or reference is created, I imagine a good js engine would 
destroy/dispose of the internal sayAlert function once it completes. */

// Create a handle/reference/instance of sayHello() using the name 'Bob'
sayHelloBob = sayHello('Bob');
sayHelloBob();

// Create another handle or reference to sayHello with a different name
sayHelloGerry = sayHello('Gerry');
sayHelloGerry();

/* Now calling them again demonstrates that each handle or reference contains its own 
unique local variable memory space. They remain in memory 'forever' 
(or until your computer/browser explode) */
sayHelloBob();
sayHelloGerry();

这表明了你应该关于关闭的两个基本概念。

在简单的说法中,我有一个基本函数,我可以创建参考或操作,其中包含独特的数据,留在记忆参考中。我不需要每次我想说别人的名字,我都需要重写函数。

我写了一篇博客帖子一段时间后解释关闭,这里是我在关闭方面所说的,为什么你会想要一个。

关闭是让一个函数有持久的,私人变量的方式 - 也就是说,只有一个函数知道的变量,在那里它可以跟踪从以前运行的时间的信息。

在这个意义上,他们让一个函数有点像一个具有私人属性的对象。

完整帖子:

那么,这些关闭事物是什么呢?

默认情况下,JavaScript知道两种类型的目标:全球和本地。

var a = 1;

function b(x) {
    var c = 2;
    return x * c;
}

在上面的代码中,变量 a 和函数 b 可从代码中的任何地方(即全球)。变量 c 仅在 b 函数范围内可用(即本地)。

JavaScript 关闭有助于解决这个问题,通过将一个函数与一个背景:

function a(x) {
    return function b(y) {
        return x + y;
    }
}

在这里,函数 a 返回一个函数称为 b. 由于 b 在 a 中定义,它会自动访问在 a 中定义的任何内容,即 x 在此示例中。

var c = a(3);

var c = function b(y) {
    return 3 + y;
}

函数b 提醒 x = 3 在其背景下,因此:

var d = c(4);

关闭也可以用来限制全球宣布的变量和方法的范围:

(function () {
    var f = "Some message";
    alert(f);
})();

现在,有一个常见的JavaScript洞穴,在那里关闭可以帮助:

var a = new Array();

for (var i=0; i<2; i++) {
    a[i]= function(x) { return x + i ; }
}

a[0] = function (x) { return x + 0 ; }
a[1] = function (x) { return x + 1 ; }
a[2] = function (x) { return x + 2 ; }

a[0] = function (x) { return x + 2 ; }
a[1] = function (x) { return x + 2 ; }
a[2] = function (x) { return x + 2 ; }

var a = new Array();

for (var i=0; i<2; i++) {
    a[i]= function(tmp) {
        return function (x) { return x + tmp ; }
    } (i);
}