All Questions

2049
votes
32answers
249375 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...
7661
votes
88answers
1167916 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...
192
votes
5answers
63005 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...
148
votes
6answers
32915 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(() =...
80
votes
3answers
6831 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...
321
votes
15answers
87167 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...
164
votes
6answers
26124 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: ...
168
votes
10answers
29450 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...
739
votes
26answers
415614 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 ...
556
votes
35answers
428996 views

Static variables in JavaScript

How can I create static variables in Javascript?

Previous Next