Help for this page

Select Code to Download


  1. or download this
    var x = "hello "
    var my_closure = function (y) {
      alert(x + y);
    }
    my_closure("world");
    
  2. or download this
    for (@some_array) {
      # Declaring this in the loop creates a new variable per
    ...
      };
    }
    # We now have one closure per array element.
    
  3. or download this
    for (var i = 0; i < some_array.length; i++) {
      // Declaring this in the loop creates a one variable for
    ...
      });
    }
    // All closures now point at the last element.
    
  4. or download this
    for (var i = 0; i < some_array.length; i++) {
      // This declares and immediately calls a function, and x
    ...
      )(some_array[i]); // And here is where we bind the value.
    }
    // We now have one closure per array element.