Access a copied integer variable in javascript anonymous method
问题 I am a C# developer and used to the way closures work in C#. Currently I have to work with anonymous javascript functions and experience a problem with the following snippet: function ClosureTest() { var funcArray = new Array(); var i = 0; while (i < 2) { var contextCopy = i; funcArray[i] = function() { alert(contextCopy); return false; }; i++; } funcArray[0](); funcArray[1](); } I expect the first funcArray() call to say 0 and the second to say 1 . However, they both say 1 . How is that