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

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


当前回答

也许我们应该切断你的27岁的朋友,因为整个“关闭”的概念是(!)...... voodoo!

我意思的是:(a)你不直觉地期望它......(b)当有人花时间向你解释它时,你肯定不会期望它工作!

当你终于意识到这样的事情是可能的,那么......肯定......任何人的后事实反应会是:“whoa-a-a(!)...... kew-el-l-l...(!!!)”

直觉给了你很多完全可靠的期望,这样的事情会“当然,绝对是无意义的,因此是完全不可能的。

正如我所说的那样,“这就是伏地。

其他回答

這是一個試圖清除一些(可能的)關於關閉的誤解,在其他答案中出現。

关闭不只是在您返回内部函数时创建。 事实上,关闭函数不需要完全返回,以便其关闭创建。 您可以将内部函数归咎于外部范围中的变量,或者将其作为一个论点转移到另一个函数,在那里它可以立即或随后被召回。

const makePlus = 函数(x) { 返回函数(y) { 返回 x + y; } const plus5 = makePlus(5); console.log(plus5(3));

如果JavaScript不知道关闭,会发生什么? 只需由其方法体(基本上是函数通话所做的事情)在最后一行中取代通话,你会得到:

console.log(x + 3);

现在, x 的定义在哪里? 我们没有在当前范围内定义它. 唯一的解决方案是让 plus5 携带其范围(或更好地说,其父母范围)。

我思考关闭的越多,我会看到它作为一个2步过程: init - 行动

init: pass first what's needed...
action: in order to achieve something for later execution.

到6岁时,我会强调关闭的实际方面:

Daddy: Listen. Could you bring mum some milk (2).
Tom: No problem.
Daddy: Take a look at the map that Daddy has just made: mum is there and daddy is here.
Daddy: But get ready first. And bring the map with you (1), it may come in handy
Daddy: Then off you go (3). Ok?
Tom: A piece of cake!

例如:把一些牛奶带到妈妈(=行动)。首先做好准备,然后把地图(=init)带到这里。

function getReady(map) {
    var cleverBoy = 'I examine the ' + map;
    return function(what, who) {
        return 'I bring ' + what + ' to ' + who + 'because + ' cleverBoy; //I can access the map
    }
}
var offYouGo = getReady('daddy-map');
offYouGo('milk', 'mum');

因为如果你带着一个非常重要的信息(地图),你有足够的知识来执行其他类似的操作:

offYouGo('potatoes', 'great mum');

对于一个开发人员来说,我会在关闭和OOP之间进行平行。 init 阶段类似于在传统 OO 语言中向构建者传达论点; 行动阶段最终是你呼吁实现你想要的方法。

请参见我的另一个答案,描述OO和关闭之间的平行性:

如何在JavaScript中“正确”创建自定义对象?

一个基本例子在JavaScript中的关闭:

关闭存储对外部函数的变量的参考:

它们不会存储实际值 关闭会变得更有趣,当关闭之前外部函数的变量变量的值被召唤时,并且这种强大的特性可以以创造性的方式利用,例如杜格拉斯·克罗克福德首次展示的这种私人变量例子:

函数名人ID( ) { var celebrityID = 999; // 我们将返回一个具有某些内部函数的对象 // 所有内部函数都有访问外部函数的变量返回 { getID:函数() { // 此内部函数将返回更新名人ID变量 // 它将返回名人ID的当前值,即使在

3、关闭已被告知



http://javascript.info/tutorial/closures http://www.javascriptkit.com/javatutors/closures.shtml

也许你应该考虑一个以对象为导向的结构而不是内部功能。

    var calculate = {
        number: 0,
        init: function (num) {
            this.number = num;
        },
        add: function (val) {
            this.number += val;
        },
        rem: function (val) {
            this.number -= val;
        }
    };

并从 calculate.number 变量中阅读结果,谁需要“返回”无论如何。

//Addition
First think about scope which defines what variable you have to access to (In Javascript);

//there are two kinds of scope
Global Scope which include variable declared outside function or curly brace

let globalVariable = "foo";

一件事要记住,一旦你宣布了一个全球变量,你可以在你的代码中的任何地方使用它,即使在功能中;

包含仅在您的代码的特定部分可用的变量的本地范围:

函数范围是当您在函数中宣布变量时,您只能在函数内访问变量。

function User(){
    let name = "foo";
    alert(name);
}
alert(name);//error

//Block scope is when you declare a variable within a block then you can  access that variable only within a block 
{
    let user = "foo";
    alert(user);
}
alert(user);
//Uncaught ReferenceError: user is not defined at.....

//A Closure

function User(fname){
    return function(lname){
        return fname + " " lname;
    }
}
let names = User("foo");
alert(names("bar"));

//When you create a function within a function you've created a closure, in our example above since the outer function is returned the inner function got access to outer function's scope