All Questions

1988
votes
32answers
240576 views

JavaScript closure inside loops – simple practical example

var funcs = []; for (var i = 0; i < 3; i++) { // let's create 3 functions funcs[i] = function() { // and store them in funcs console.log("My value: " + i); // each should...
7663
votes
88answers
1142057 views

How do JavaScript closures work?

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I h...
187
votes
5answers
61161 views

Javascript infamous Loop issue?

I've got the following code snippet. function addLinks () { for (var i=0, link; i<5; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.oncl...
143
votes
6answers
32022 views

Captured variable in a loop in C#

I met an interesting issue about C#. I have code like below. List<Func<int>> actions = new List<Func<int>>(); int variable = 0; while (variable < 5) { actions.Add(() =...
314
votes
15answers
84511 views

What is the purpose of a self executing function in javascript?

In javascript, when would you want to use this: (function(){ //Bunch of code... })(); over this: //Bunch of code...
80
votes
3answers
6568 views

Local variables in Python nested functions

Okay, bear with me on this, I know it's going to look horribly convoluted, but please help me understand what's happening. from functools import partial class Cage(object): def __init__(self, an...
162
votes
6answers
24949 views

What do (lambda) function closures capture?

Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code: adders=[0,1,2,3] for i in [0,1,2,3]: adders[i]=lambda a: ...
165
votes
10answers
28845 views

What are 'closures' in .NET?

What is a closure? Do we have them in .NET? If they do exist in .NET, could you please provide a code snippet (preferably in C#) explaining it? EDIT: I went through Jon Skeet's article to understan...
718
votes
26answers
404457 views

Why are Python lambdas useful?

I'm trying to figure out Python lambdas. Is lambda one of those "interesting" language items that in real life should be forgotten? I'm sure there are some edge cases where it might be needed, but ...
546
votes
35answers
416174 views

Static variables in JavaScript

How can I create static variables in Javascript?

Previous Next