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

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


当前回答

(我没有考虑到6年前的事情。

在JavaScript等语言中,您可以将函数作为参数转移到其他函数(函数是第一级公民的语言),您往往会发现自己正在做这样的事情:

var name = 'Rafael';

var sayName = function() {
  console.log(name);
};

你可以看到, sayName 没有定义的名称变量,但它使用的名称值被定义在 sayName 之外(在母范围内)。

functionThatTakesACallback(sayName);

请注意:

接下来会发生什么? 参考错误:未定义的名称?

因此: 即使名称不在函数 sayName 将被召唤的范围内(函数ThatTakesACallback 内), sayName 可以访问与 sayName 相关的关闭中捕获的名称值。

───

其他回答

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

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

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

关闭仅仅是当一个函数进入其外部范围后,即使该范围的函数完成执行。

function multiplier(n) {
    function multiply(x) {
          return n*x;
    }
    return mutliply;
}

var 10xmultiplier = multiplier(10);
var x = 10xmultiplier(5); // x= 50

我们可以看到,即使复合器完成执行后,内部函数复合器仍然可以访问这个例子中的 x 的值,即 10。

一个非常常见的使用关闭是流动(上面的同样的例子),在那里我们逐步用参数来完善我们的功能,而不是同时提供所有论点。

我们可以实现这一点,因为JavaScript(除了原型OOP之外)允许以功能的方式编程,在那里更高的命令功能可以采取其他功能作为论点(分类功能)。

我强烈建议你阅读这本书由Kyle Simpson: 2 一部分的书系列是专门关闭,它被称为范围和关闭。

我倾向于通过好/坏的比较更好地学习,我喜欢看到工作代码跟随不工作的代码,有人可能会遇到,我把一个 jsFiddle 组合起来,它进行比较,并试图将差异推向我能找到的最简单的解释。

关闭是正确的:

console.log('CLOSURES DONE RIGHT');

var arr = [];

function createClosure(n) {
    return function () {
        return 'n = ' + n;
    }
}

for (var index = 0; index < 10; index++) {
    arr[index] = createClosure(index);
}

for (var index of arr) {
    console.log(arr[index]());
}

关闭错误:

console.log('CLOSURES DONE WRONG');

function createClosureArray() {
    var badArr = [];

    for (var index = 0; index < 10; index++) {
        badArr[index] = function () {
            return 'n = ' + index;
        };
    }
    return badArr;
}

var badArr = createClosureArray();

for (var index of badArr) {
    console.log(badArr[index]());
}

在上面的代码中,路径在创建ClosureArray()函数中移动,函数现在只会返回完成的路径,这在第一眼看起来更直观。

结果

在模块模式中,您定义了一个函数,然后立即在所谓的即时启动函数表达(IIFE)中呼叫它。 您在该函数中所写的一切都有私人范围,因为它在关闭中定义,从而允许您在JavaScript中“模拟”隐私。

 var Closure = (function () {
    // This is a closure
    // Any methods, variables and properties you define here are "private"
    // and can't be accessed from outside the function.

    //This is a private variable
    var foo = "";

    //This is a private method
    var method = function(){

    }
})();

另一方面,如果您希望在关闭室外看到一个或多个变量或方法,您可以将其返回对象内部。

var Closure = (function () {
  // This is a closure
  // Any methods, variables and properties you define here are "private"
  // and can't be accessed from outside the function.

  //This is a private variable
  var foo = "";

  //This is a private method
  var method = function(){

  }

  //The method will be accessible from outside the closure
  return {
    method: method
  }

})();

Closure.method();

希望能帮忙......看,

关闭是通过内部函数可以提到在其外部关闭函数中存在的变量,其母函数已经结束后。

// A function that generates a new function for adding numbers.
function addGenerator( num ) {
    // Return a simple function for adding two numbers
    // with the first number borrowed from the generator
    return function( toAdd ) {
        return num + toAdd
    };
}

// addFive now contains a function that takes one argument,
// adds five to it, and returns the resulting number.
var addFive = addGenerator( 5 );
// We can see here that the result of the addFive function is 9,
// when passed an argument of 4.
alert( addFive( 4 ) == 9 );