Having coded JavaScript professionally and enjoyed it thoroughly for 6+ years now, I respectfully degree. The Harmony project shows just how much work there is to be done to make JS more fun/practical to use day in and day out.
Javascript itself has very few faults.
We disagree.Signed,
Douglas Crockford, Jeremy Ashkenas, and Brendan Eich
foo = function(){
var bar=0
return function(){
bar=bar+1
return bar
}
}
baz=foo()
baz() // => 1
baz() // => 2
baz() // => 3
bar; // => ERROR: bar is not definedAnd nested loops that both use "i" would work.
It does work like C. The scoping you're talking about was introduced in C99.
You might expect it to alert 1..2..3..etc. It actually alerts 10..10..10..etc.
My intended point was that if Javascript had true lexical scope then each iteration of the loop would create a distinct variable. A reference to that variable would then be captured by each closure. You can simulate the behavior of lexical scoping by "abusing" closures as someone mentioned before. Like this:
for ( var i = 1; i < 10; ++i ) ( function( i ) { setTimeout( function( ) { alert( i ) }, i * 250 ) } )( i )
I wonder about the word "abusing" here. Languages that have block-level lexical scoping (I hope I am using the correct word) sometimes implement block scoping by renaming the variables and hoisting them to the enclosing function, and they sometimes implement block scoping by creating a new closure just as we do by hand in JavaScript, and then hoisting the result.
For example, naïve implementations of Scheme give you a "let" macro that is expanded into the closure form you give above, and then an optimizing step comes along a little later and performs lambda hoisting for you. So this "abusing" we are doing is what the language would have done for us in certain implementations anyways!
for (var i = 1; i < 10; ++i)
{
with({i:i}) // let (i=i)
{
setTimeout( function( ) { alert( i ) }, i * 250 );
}
}
will do what you intend. If let is available, for (let i = 1; etc. will do the same thing without the cruft.EDIT: http://pastebin.com/7BUXKVXT
Paste it into Racket and choose language R5RS.
(define-syntax for
(syntax-rules ()
((for ((var init) condition step) expr ...)
(let ((var init))
(let loop ()
(if condition
(begin
(begin expr ...)
step
(loop))))))))
(define (test-for)
(let ((v (make-vector 10)))
(for ((i 0) (< i 10) (set! i (+ 1 i)))
(vector-set! v i (lambda () i)))
(for ((i 0) (< i 10) (set! i (+ 1 i)))
(display ((vector-ref v i)))
(newline))))This reminds me of bug in the Visual C++ 6.0 compiler, where a variable declared in the "head" of a for loop would continue to exist after the loop scope had closed. Javascript has exactly the same problem, but by design, due to only having global and function-level scope.
It would be the same variable with or without lexical scope (by that I mean block-level).
Personally, I would like to be able to choose the capture method, but having value capture the default. Most of my code simply wants the value, but occasionally, capturing the variable is needed.
dosomethingasync =>
#etc
will automagically bind the closure to the current value of this.From then on, I use 'that' instead of 'this', relying on closure magic to make things work. You never have to worry about how the function is bound thereafter.
CoffeeScript is cool, as it is basically "Python in JS" but I have some resistance against languages that convert to other languages. I'm not so sure why.
That's mostly just true for browsers. Javascript itself has very few faults.
My biggest annoyances with the language are: Lack of lexical scope, no trailing commas and semicolon usage that differs from C.
I could complain all day about my annoyances with browsers (or just IE).