如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
当前回答
孩子们永远不会忘记他们与父母分享的秘密,即使父母离开后。
JavaScript 功能的秘密是私人变量
var parent = function() {
var name = "Mary"; // secret
}
正如你可以猜测的那样,因为变量每次函数被召唤,没有人会知道它们,必须有一个秘密的地方,它们被存储在那里,它可以被称为秘密室或管道或地方范围,但这并不重要,我们知道它们在某个地方隐藏在记忆中。
但是,在JavaScript中,这是一个非常特殊的事情,在其他功能内创建的功能,也可以知道父母的本地变量,并保持它们,只要他们活着。
var parent = function() {
var name = "Mary";
var child = function(childName) {
// I can also see that "name" is "Mary"
}
}
因此,只要我们在父母功能,它可以创建一个或多个儿童功能,从秘密位置分享秘密变量。
但可悲的是,如果孩子也是父母功能的私人变量,当父母结束时也会死亡,而秘密也会与他们一起死亡。
所以要活着,孩子必须离开,直到太晚了。
var parent = function() {
var name = "Mary";
var child = function(childName) {
return "My name is " + childName +", child of " + name;
}
return child; // child leaves the parent ->
}
var child = parent(); // < - and here it is outside
所以,如果你叫孩子“爱丽丝”,她会回答。
child("Alice") => "My name is Alice, child of Mary"
这就是所有要说的。
其他回答
对于一个六岁的...
你知道什么是物体吗?
物品是具有属性和物品的物品。
關閉的最重要的事情之一是,它們讓你在JavaScript中創建對象. 在JavaScript中的對象只是功能和關閉,讓JavaScript在創建後儲存對象的屬性價值。
物品非常有用,并保持一切顺利和有组织,不同物品可以做不同的工作,工作物品可以做复杂的事情。
幸运的是,JavaScript有创建对象的关闭,否则一切都会变成一个可怕的噩梦。
想象一下,在你的城市里有一个非常大的公园,你会看到一个名叫Coder先生的魔法师在公园的不同角落开始棒球游戏,使用他的魔法窗口,称为JavaScript。
当然,每个棒球游戏都有相同的规则,每个游戏都有自己的分数板。
当然,一个棒球游戏的分数与其他游戏完全分开。
关闭是Coder先生保持所有他的魔法棒球游戏的分数的特殊方式。
我确信,爱因斯坦没有直接期望我们选择任何异常的脑震动的东西,并在六岁时与无用的尝试,让这些“疯狂”(甚至更糟糕的是他们)的东西到他们的童年头脑:)如果我六岁,我不会喜欢这样的父母,也不会与这样的无聊的慈善家建立友谊,对不起。
无论如何,对于婴儿,关闭只是一个拥抱,我猜,无论你如何试图解释:)当你拥抱一个朋友的,然后你两种分享你男孩现在有什么东西。
我真的不知道如何向5至6岁的婴儿解释,我也不认为他们会欣赏任何JavaScript代码剪辑,如:
function Baby(){
this.iTrustYou = true;
}
Baby.prototype.hug = function (baby) {
var smiles = 0;
if (baby.iTrustYou) {
return function() {
smiles++;
alert(smiles);
};
}
};
var
arman = new Baby("Arman"),
morgan = new Baby("Morgana");
var hug = arman.hug(morgan);
hug();
hug();
只为儿童:
关闭是拥抱
波格飞了
亲吻是微妙的!:)
第一点的例子 由 dlaliberte:
关闭不只是在您返回内部函数时创建。 事实上,关闭函数根本不需要返回. 您可以将内部函数归咎于外部范围中的变量,或者将其作为一个论点转移到另一个函数,在那里它可以立即使用。
var i;
function foo(x) {
var tmp = 3;
i = function (y) {
console.log(x + y + (++tmp));
}
}
foo(2);
i(3);
好吧,和一个6岁的孩子谈话,我可能会使用随后的协会。
想象一下 - 你正在玩你的小兄弟姐妹在整个房子,你正在移动周围与你的玩具,并将其中的一些带到你的哥哥的房间。 过了一会儿,你的哥哥从学校回来,去了他的房间,他锁在里面,所以现在你不能访问玩具留在那里再直接的方式。
比较一个情况,当一个门被草案锁定,没有人在里面(通用功能执行),然后一些当地的火灾发生并燃烧房间(垃圾收集器:D),然后一个新的房间被建造,现在你可以留下其他玩具在那里(新功能例子),但从来没有得到相同的玩具留在第一间房间例子。
对于一个先进的孩子,我会把这样的东西放在下面,这不是完美的,但它让你感觉到它是什么:
function playingInBrothersRoom (withToys) {
// We closure toys which we played in the brother's room. When he come back and lock the door
// your brother is supposed to be into the outer [[scope]] object now. Thanks god you could communicate with him.
var closureToys = withToys || [],
returnToy, countIt, toy; // Just another closure helpers, for brother's inner use.
var brotherGivesToyBack = function (toy) {
// New request. There is not yet closureToys on brother's hand yet. Give him a time.
returnToy = null;
if (toy && closureToys.length > 0) { // If we ask for a specific toy, the brother is going to search for it.
for ( countIt = closureToys.length; countIt; countIt--) {
if (closureToys[countIt - 1] == toy) {
returnToy = 'Take your ' + closureToys.splice(countIt - 1, 1) + ', little boy!';
break;
}
}
returnToy = returnToy || 'Hey, I could not find any ' + toy + ' here. Look for it in another room.';
}
else if (closureToys.length > 0) { // Otherwise, just give back everything he has in the room.
returnToy = 'Behold! ' + closureToys.join(', ') + '.';
closureToys = [];
}
else {
returnToy = 'Hey, lil shrimp, I gave you everything!';
}
console.log(returnToy);
}
return brotherGivesToyBack;
}
// You are playing in the house, including the brother's room.
var toys = ['teddybear', 'car', 'jumpingrope'],
askBrotherForClosuredToy = playingInBrothersRoom(toys);
// The door is locked, and the brother came from the school. You could not cheat and take it out directly.
console.log(askBrotherForClosuredToy.closureToys); // Undefined
// But you could ask your brother politely, to give it back.
askBrotherForClosuredToy('teddybear'); // Hooray, here it is, teddybear
askBrotherForClosuredToy('ball'); // The brother would not be able to find it.
askBrotherForClosuredToy(); // The brother gives you all the rest
askBrotherForClosuredToy(); // Nothing left in there
正如你可以看到的那样,房间里留下的玩具仍然可以通过兄弟访问,不管房间是否被锁定。