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

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


当前回答

对于一个六岁的...

你知道什么是物体吗?

物品是具有属性和物品的物品。

關閉的最重要的事情之一是,它們讓你在JavaScript中創建對象. 在JavaScript中的對象只是功能和關閉,讓JavaScript在創建後儲存對象的屬性價值。

物品非常有用,并保持一切顺利和有组织,不同物品可以做不同的工作,工作物品可以做复杂的事情。

幸运的是,JavaScript有创建对象的关闭,否则一切都会变成一个可怕的噩梦。

其他回答

能否向五岁男孩解释关闭?

我仍然认为谷歌的解释工作得很好,简要:

/*
*    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 = 'Hey!';
    var content = document.getElementById('content');
    function innerFunction() {
        content.innerHTML = someNum + ': ' + someString;
        content = null; // Internet Explorer memory leak for DOM reference
    }
    innerFunction();
}

outerFunction(1);​

此分類上一篇

答:C#问题

好吧,6岁的关闭粉丝,你想听到最简单的关闭例子吗?

让我们想象下一个情况:司机坐在车里,那辆车在飞机内,飞机在机场,司机的能力在车外,但在飞机内,即使飞机离开机场,也是一个关闭。

这就是我如何将我的飞机故事转换为代码。

飞机 = 函数(默认空港) { var lastAirportLeft = 默认空港; var car = { 司机: { startAccessPlaneInfo: 函数() { setInterval(函数() { console.log(“最后空港是“ + lastAirportLeft); }, 2000); } }; car.driver.startAccessPlaneInfo(); return { leaveTheAirport: 函数(airPortName) {

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

此外,必须明白,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();

最简单、最短、最容易理解的答案:

关闭是一个代码块,每个行都可以用相同的变量名称引用相同的变量集。

如果“这”意味着与它在其他地方做什么不同,那么你知道它是两个不同的关闭。

function sing(person) {

    var firstPart = "There was " + person + " who swallowed ";

    var fly = function() {
        var creature = "a fly";
        var result = "Perhaps she'll die";
        alert(firstPart + creature + "\n" + result);
    };

    var spider = function() {
        var creature = "a spider";
        var result = "that wiggled and jiggled and tickled inside her";
        alert(firstPart + creature + "\n" + result);
    };

    var bird = function() {
        var creature = "a bird";
        var result = "How absurd!";
        alert(firstPart + creature + "\n" + result);
    };

    var cat = function() {
        var creature = "a cat";
        var result = "Imagine That!";
        alert(firstPart + creature + "\n" + result);
    };

    fly();
    spider();
    bird();
    cat();
}

var person="an old lady";

sing(person);

指示

我没有时间去购物,所以函数需要知道我们在冰箱里有什么来做出决定。 每个成分都有不同的烹饪时间,我们希望所有的东西都被机器人同时加热。 我们需要提供函数与我们喜欢的数据,函数可以“说话”到冰箱,函数可以控制机器人。

function cookMeal() {  /*  STUFF INSIDE THE FUNCTION  */  }

function sing(person) {  /* STUFF INSIDE THE FUNCTION */  }

因此,这个功能可能与歌曲有关系,可能需要一些关于一个人的数据。

现在,在函数歌(),接近代码的结尾是线

var person="an old lady";

全球变量:人是一个全球变量,这意味着如果你将其值从“老太太”变成“年轻人”,这个人将继续是年轻人,直到你决定再次改变它,而代码中的任何其他功能可以看到它是一个年轻人。

sing(person);

这条线叫这个功能,就好像它叫一个狗。

关闭后,线路来了。

fly();
spider();
bird();
cat();