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

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


当前回答

你和你的家人住在神话般的Ann Ville城,你有一个朋友住在门旁,所以你打电话给他们,请他们出去玩。

000001(JamiesHouse)

一个月后,你和你的家人从Ann Ville出发到下一个城市,但你和你的朋友仍然保持联系,所以现在你必须对你的朋友居住的城市的区域代码说话,然后说话他们的“正确”号码:

001 000001 (annVille.jamiesHouse)

01 001 000001 (myOldCountry.annVille.jamiesHouse)

事实上,如此可怕,你告诉你从你的老国家的杰米......你有一个好笑话,所以有一天,你和你的家人去度假回到老国家,你去参观你的老城(安·维尔),去参观杰米。

“真的?另一个杰米?在安·维尔?在你的新国家!!!?”“是的......让我们叫他们......”

观点?

其他回答

我只是将它们指向Mozilla关闭页面,这是我发现的关闭基础和实用使用的最佳,最简短和简单的解释,强烈建议任何人学习JavaScript。

而且是的,我甚至会推荐给一个6岁的人 - 如果6岁的人正在学习关闭,那么他们准备好理解文章中提供的简短和简单的解释。

关闭是函数被关闭时,函数被定义为名称空间,函数被召唤时是不可变的。

在JavaScript中,它发生时:

定义一个函数在另一个函数内 内部函数在外部函数返回后被召回

// 'name' is resolved in the namespace created for one invocation of bindMessage
// the processor cannot enter this namespace by the time displayMessage is called
function bindMessage(name, div) {

    function displayMessage() {
        alert('This is ' + name);
    }

    $(div).click(displayMessage);
}

如果该函数包含类似于呼叫回复函数,则呼叫回复函数仍在范围内;如果呼叫回复函数在母函数的即时环境中提到某些本地变量,那么当然,你会期望该变量无法访问呼叫回复函数并返回未定义。

关闭确保通过召回函数提到的任何属性都可用于该函数的使用,即使其母函数可能已经失效。

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

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

默认情况下,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);
}