您如何向知道其所包含概念的人(例如函数,变量等)解释JavaScript闭包,但不了解闭包本身?
我已经看过维基百科上给出的Scheme示例,但遗憾的是它并没有帮助.
var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
// and store them in funcs
funcs[i] = function() {
// each should log its value.
console.log("My value: " + i);
};
}
for (var j = 0; j < 3; j++) {
// and now let's run each one to see
funcs[j]();
}Run Code Online (Sandbox Code Playgroud)
它输出这个:
我的价值:3
我的价值:3
我的价值:3
而我希望它输出:
我的价值:0
我的价值:1
我的价值:2
使用事件侦听器导致运行函数的延迟时,会出现同样的问题:
var buttons = document.getElementsByTagName("button");
// let's create 3 …Run Code Online (Sandbox Code Playgroud)我想弄清楚Python lambdas.lambda是现实生活中应该被遗忘的"有趣"语言项目之一吗?
我确信有一些可能需要它的边缘情况,但考虑到它的模糊性,它在未来版本中重新定义的可能性(我基于它的各种定义的假设)和降低的编码清晰度 - 应该是要避免吗?
这让我想起C类型的溢出(缓冲区溢出) - 指向顶部变量并重载以设置其他字段值.感觉就像是一种技术表演,但维护编码器的噩梦.
有人能解释一下吗 我理解它们背后的基本概念,但我经常看到它们互换使用,我感到困惑.
现在我们在这里,它们与常规功能有什么不同?
我的一个朋友和我正在讨论什么是JS的封闭,什么不是.我们只是想确保我们真正理解它.
我们来看看这个例子吧.我们有一个计数循环,并希望在控制台上打印计数器变量延迟.因此,我们使用setTimeout和闭包来捕获计数器变量的值,以确保它不会打印值N的N倍.
错误的解决方案,无需关闭或接近任何倒闭将是:
for(var i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
Run Code Online (Sandbox Code Playgroud)
这当然会打印10次i循环后的值,即10.
所以他的尝试是:
for(var i = 0; i < 10; i++) {
(function(){
var i2 = i;
setTimeout(function(){
console.log(i2);
}, 1000)
})();
}
Run Code Online (Sandbox Code Playgroud)
按预期打印0到9.
我告诉他,他并没有使用封闭捕获i,但他坚持认为他是.我证明他没有使用闭包,将for循环体放在另一个setTimeout(将他的匿名函数传递给setTimeout),再次打印10次10.如果我将他的函数存储在a中var并在循环之后执行它同样适用,也打印10次10.所以我的论点是他并没有真正捕获它的值i,使他的版本不是一个闭包.
我的尝试是:
for(var i = 0; i < …Run Code Online (Sandbox Code Playgroud) 在javascript中,你想什么时候使用它:
(function(){
//Bunch of code...
})();
Run Code Online (Sandbox Code Playgroud)
对此:
//Bunch of code...
Run Code Online (Sandbox Code Playgroud) 我正在检查一些PHP 5.3.0功能,并在网站上遇到一些看起来很有趣的代码:
public function getTotal($tax)
{
$total = 0.00;
$callback =
/* This line here: */
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
return round($total, 2);
}
Run Code Online (Sandbox Code Playgroud)
作为匿名函数的一个例子.
有人知道吗?有文件吗?如果它被使用它看起来很邪恶?
我在WebKit HTML 5 SQL存储笔记演示的源代码中看到以下内容:
function Note() {
var self = this;
var note = document.createElement('div');
note.className = 'note';
note.addEventListener('mousedown', function(e) { return self.onMouseDown(e) }, false);
note.addEventListener('click', function() { return self.onNoteClick() }, false);
this.note = note;
// ...
}
Run Code Online (Sandbox Code Playgroud)
笔者采用自我在一些地方(函数体)及本在其他地方(的函数方法的参数列表中定义的机构).这是怎么回事?现在我已经注意到了它,我会在各处开始看到它吗?
string [] files = new string[2];
files[0] = "ThinkFarAhead.Example.Settings.Configuration_Local.xml";
files[1] = "ThinkFarAhead.Example.Settings.Configuration_Global.xml";
//Resharper complains this is an "access to modified closure"
for (int i = 0; i < files.Length; i++ )
{
// Resharper disable AccessToModifiedClosure
if(Array.Exists(Assembly.GetExecutingAssembly().GetManifestResourceNames(),
delegate(string name) { return name.Equals(files[i]); }))
return Assembly.GetExecutingAssembly().GetManifestResourceStream(files[i]);
// ReSharper restore AccessToModifiedClosure
}
Run Code Online (Sandbox Code Playgroud)
虽然ReSharper抱怨这是"访问修改后的闭包",但上述情况似乎工作正常.任何人都可以阐明这一点吗?
(这个主题在这里继续)