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

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


当前回答

关闭是简单的

你可能不应该告诉一个六岁的关闭,但如果你这样做,你可能会说,关闭给了一个能力获得访问一个变量宣布在某些其他功能范围。

此分類上一篇

函数 getA() { var a = []; // 此操作发生后, // 函数返回后 // 函数的 `a` 值设置Timeout(函数() { a.splice(0, 0, 1, 2, 3, 4, 5); }); 返回 a; } var a = getA(); 出(‘什么是‘a` 长度?’); 出(‘a` 长度是‘ + a. 长度’); 设置Timeout(函数() { out(‘No wait...’); 出(‘a` 长度是‘ + a. 长度’); 出(‘OK : <unk> 长度’ )); <pre id="output"></

其他回答

TLDR

细节

当一个函数通过内部(呼叫)方法引用时,函数对象的环境参考将复制到新创建的执行框架(stack frame)环境记录的外部环境参考。

在下面的例子中,函数f 关闭了全球执行背景的语法环境:

function f() {}

在下面的例子中,函数h在函数g的语法环境中关闭,而函数h则在全球执行背景的语法环境中关闭。

function g() {
    function h() {}
}

如果内部函数由外部函数返回,则外部语法环境在外部函数返回后将继续存在,因为如果内部函数最终被召回,则外部语法环境必须可用。

在JavaScript中,关闭是令人惊叹和独特的,其中变量或论点可用于内部功能,即使外部功能已返回后,它们也会活着。

function getFullName(a, b) {
  return a + b;
}

function makeFullName(fn) {

  return function(firstName) {

    return function(secondName) {

      return fn(firstName, secondName);

    }
  }
}

makeFullName(getFullName)("Stack")("overflow"); // Stackoverflow

认真对待这个问题,我们应该知道什么一个典型的6岁的人能够认知,但承认的是,一个对JavaScript感兴趣的人并不那么典型。

关于童年发展: 5 至 7 年,它说:

我们可以在JavaScript中编码此类内容:

函数 makeKitchen() { var trashBags = ['A', 'B', 'C']; // 只有 3 在第一次返回 { getTrashBag: 函数() { return trashBags.pop(); }; } var 厨房 = makeKitchen(); console.log(kitchen.getTrashBag()); // 返回垃圾袋 C console.log(kitchen.getTrashBag()); // 返回垃圾袋 B console.log(kitchen.getTrashBag()); // 返回垃圾袋 A

每次做Kitchen() 被召唤,一个新的关闭创建与自己的单独的废物Bags. 废物Bags 变量是本地到每个厨房的内部,并不能在外面访问,但内部功能在 getTrashBag 属性有访问它。

因为所有这些外部变量,由一个经文所指的函数,实际上是其经文所关闭的函数链中的本地变量(全球变量可以假设是某种根函数的本地变量),并且每个函数的单一执行都会产生其本地变量的新例子,因此,每个函数的执行都会返回(或以其他方式转换)。

此外,必须明白,JavaScript中的本地变量不是在滑板框中创建的,而是在滑板上,只有当没有人提到它们时才会被摧毁。当一个函数返回时,其本地变量的参考会减少,但如果在当前执行期间,它们成为关闭的一部分,并且仍然被其经文所定义的函数所提到。

一个例子:

function foo (initValue) {
   //This variable is not destroyed when the foo function exits.
   //It is 'captured' by the two nested functions returned below.
   var value = initValue;

   //Note that the two returned functions are created right now.
   //If the foo function is called again, it will return
   //new functions referencing a different 'value' variable.
   return {
       getValue: function () { return value; },
       setValue: function (newValue) { value = newValue; }
   }
}

function bar () {
    //foo sets its local variable 'value' to 5 and returns an object with
    //two functions still referencing that local variable
    var obj = foo(5);

    //Extracting functions just to show that no 'this' is involved here
    var getValue = obj.getValue;
    var setValue = obj.setValue;

    alert(getValue()); //Displays 5
    setValue(10);
    alert(getValue()); //Displays 10

    //At this point getValue and setValue functions are destroyed
    //(in reality they are destroyed at the next iteration of the garbage collector).
    //The local variable 'value' in the foo is no longer referenced by
    //anything and is destroyed too.
}

bar();

标签:猴子

在上面的例子中,outerFunction 被称为,其次称为 innerFunction. 请注意,outerVar 是如何向 innerFunction 提供的,通过正确警告 outerVar 的值来证明。

标签:猴子

referenceToInnerFunction 设置为 outerFunction(),它简单地返回一个参考到 innerFunction. 当 referenceToInnerFunction 被召回时,它返回 outerVar. 再次,如上所述,这表明 innerFunction 有访问 outerVar,一个变量 outerFunction. 此外,值得注意的是,它保留这个访问,即使 outerFunction 完成执行后。

警告:猴子警告:猴子警告

關閉的兩件事要注意:第一,關閉總是會有其內容功能的最後價值的存取。

函数外( ) 函数( ) 函数( ) 函数( ) 函数( ) 函数( ) 函数( ) 函数( ) 函数( ) 函数( )