[Javascript] Help with closures

Bill Moseley moseley at hank.org
Fri Jan 12 15:29:25 CST 2007


On Fri, Jan 12, 2007 at 08:00:15PM +0000, Nick Fitzsimons wrote:
> function testWithBlock() {
>    var bar = 1;
>    for (var i = 0; i < 10; i++) {
>       var bar = i; // this is just redefining the outer bar, not a  new bar with block scope
>    }
>    alert(bar); // shows "9"
> }

So is this exactly the same then (note lack of var on inner bar)?:

    function testWithBlock() {
       var bar = 1;
       for (var i = 0; i < 10; i++) {
          bar = i;
       }
       alert(bar); // shows "9"
    }

Kind of disturbing, if so. ;)

> I would suggest having a look at ECMA 262, which  
> defines the ECMAScript language of which JavaScript is an  
> implementation, but nobody who's read it all the way through (several  
> times) would willingly inflict it on another human being :-)

I'd like to, but I'm enjoying poking hot sticks in my eyes too much.


> HTH,

It does.  Thanks much.

-- 
Bill Moseley
moseley at hank.org




More information about the Javascript mailing list