Remember JScript closures capture all variables in scope

Just a gentle reminder to readers, that different languages will capture outer variable with their own nuances, and sometimes these will make a difference.

So, for example, C# will capture only what’s needed in the inner function. See this post from Bill Wagner for a great discussion on the topic. Raymond Chen also covers this here.

One gotcha found in C# and VB.Net, for example, is that there is a single closure per scope, so if you have multiple functions capturing variables, they will all get the same scope.

ECMAScript also has some gotchas. In particular, just declaring the inner function is enough for all variables in scope to be captured, regardless of whether they are ever used.

You can try it right now in your address bar (yes, another silly trick).

javascript:(function(b){var a=1;var f=function(){alert(eval(“a+b”));};f();})(2)

Here is the formatted version of the code:

(function(b) {
  var a=1;
  var f=function() {

Running this will product a messagebox displaying ‘3’. So here we have a function that doesn’t actually reference any variables directly, but they are still in scope because ‘eval’ can find them. Yes, parameters are included as well – they are function variables for all practical purposes.

If you had other locals in the function, for example a large array, that wouldn’t be collected until the inner function was out of scope for the program, and you’d have to remove the reference ‘by hand’ by setting it to null for example.


Comments (0)

Skip to main content