如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
当前回答
查找描述的解释:如何在场景背后的JavaScript关闭工作。
文章解释了范围对象(或LexicalEnvironments)如何分配并以直观的方式使用。
"use strict";
var foo = 1;
var bar = 2;
function myFunc() {
//-- Define local-to-function variables
var a = 1;
var b = 2;
var foo = 3;
}
//-- And then, call it:
myFunc();
在执行顶级代码时,我们有以下范围对象的安排:
此分類上一篇
当 myFunc() 被召唤时,我们有以下范围链:
此分類上一篇
了解范围对象是如何创建,使用和删除,这是一个关键,有一个大图像,并了解如何在盖子下工作关闭。
请参见上述文章,详细信息。
其他回答
这个答案是这个YouTube视频JavaScript关闭的总结。
关闭只是保持其私人变量的状态的国家功能。
此分類上一篇
但现在有情况下,我们希望保持这个功能状态,即JavaScript关闭会使用。 关闭是内部函数的功能,如下代码所示的返回通话。
此分類上一篇
function Counter() {
var counter = 0;
var Increment = function () {
counter++;
alert(counter);
}
return {
Increment
}
}
var x = Counter(); // get the reference of the closure
x.Increment(); // Displays 1
x.Increment(); // Display 2 ( Maintains the private variables)
但现在最大的问题是如何使用这种可靠的功能。 可靠的功能是构建区块,以实施OP概念,如抽象、包容和创建自我包含的模块。
因此,任何你想要的包容,你可以把它作为私人和事物被曝光给公众应该放回声明。 此外,这些组件是自我包含的隔离物体,所以它们不会污染全球变量。
遵循 OOP 原则的对象是自我包含的,遵循抽象,遵循包容等。
此分類上一篇
我知道已经有很多解决方案,但我猜这个小而简单的脚本可以有用来证明这个概念:
// makeSequencer will return a "sequencer" function
var makeSequencer = function() {
var _count = 0; // not accessible outside this function
var sequencer = function () {
return _count++;
}
return sequencer;
}
var fnext = makeSequencer();
var v0 = fnext(); // v0 = 0;
var v1 = fnext(); // v1 = 1;
var vz = fnext._count // vz = undefined
认真对待这个问题,我们应该知道什么一个典型的6岁的人能够认知,但承认的是,一个对JavaScript感兴趣的人并不那么典型。
关于童年发展: 5 至 7 年,它说:
我们可以在JavaScript中编码此类内容:
函数 makeKitchen() { var trashBags = ['A', 'B', 'C']; // 只有 3 在第一次返回 { getTrashBag: 函数() { return trashBags.pop(); }; } var 厨房 = makeKitchen(); console.log(kitchen.getTrashBag()); // 返回垃圾袋 C console.log(kitchen.getTrashBag()); // 返回垃圾袋 B console.log(kitchen.getTrashBag()); // 返回垃圾袋 A
每次做Kitchen() 被召唤,一个新的关闭创建与自己的单独的废物Bags. 废物Bags 变量是本地到每个厨房的内部,并不能在外面访问,但内部功能在 getTrashBag 属性有访问它。
好吧,和一个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
正如你可以看到的那样,房间里留下的玩具仍然可以通过兄弟访问,不管房间是否被锁定。
第一個問題有一個引用:
如果你不能向六岁的人解释,你真的不明白。
这就是我会试图向一个真正的六岁男孩解释的方式:
你知道成年人如何拥有房子,他们称之为房子吗?当一个母亲有孩子时,孩子真的没有任何东西,对吗?但是她的父母拥有房子,所以每当有人问“你的房子在哪里?”孩子可以回答“那房子!”并指向父母的房子。
“关闭”是孩子能够永远(即使在国外)提到自己的家,即使是真正的父母拥有家。