Fogus: Node.js should become its own language
11–20 of 79 posts
Re: Fogus: Node.js should become its own language
#12"My problem with Javascript has always been that expertise is defined more in terms of understanding its faults rather than its features." 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).
Except that Javascript has lexical scope. Use the var keyword to define local variables. 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 defined
Re: Fogus: Node.js should become its own language
#13Why would you write this: [long_io_operation with callback] When you can instead write this: [long_io_operation without callback] Off the top of my head, I'd say because "var result = long_io_operation(req);" is not only not self-documenting but actively deceptive if long_io_operation is an asynchronous call. I'm not sure I see what's to be gained by pretending closures don't exist.
Re: Fogus: Node.js should become its own language
#14"My problem with Javascript has always been that expertise is defined more in terms of understanding its faults rather than its features." 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).
My most common annoyance about Javascript is the lack of bound methods like in Python Ruby etc. When you specify callbacks, you always have to provide the value of 'this' as well. If you forget, it's basically random and you're in for some heavy debugging.
dosomethingasync =>
#etc
will automagically bind the closure to the current value of this.Re: Fogus: Node.js should become its own language
#15Earlier quoted context omitted.
Except that Javascript has lexical scope. Use the var keyword to define local variables. 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 defined
Try this: for ( var i = 1; i You might expect it to alert 1..2..3..etc. It actually alerts 10..10..10..etc.
Re: Fogus: Node.js should become its own language
#16Re: Fogus: Node.js should become its own language
#17For me, more interesting than a pure assault on callbacks would be to see if you could handle more asynchronous scenarios, using async.js as a baseline.
e.g. callback waterfall, callback when all children are finished, callback when any children are finished
Re: Fogus: Node.js should become its own language
#18"My problem with Javascript has always been that expertise is defined more in terms of understanding its faults rather than its features." 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).
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.
Re: Fogus: Node.js should become its own language
#19Earlier quoted context omitted.
Try this: for ( var i = 1; i You might expect it to alert 1..2..3..etc. It actually alerts 10..10..10..etc.
That's because closures actually capture the variable, not the current value of it. They'd be a lot less useful if they just made a copy of the variables...
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 )
Re: Fogus: Node.js should become its own language
#20Earlier quoted context omitted.
That's because closures actually capture the variable, not the current value of it. They'd be a lot less useful if they just made a copy of the variables...
I'm sorry my original post wasn't very clear. 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( fun…
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.