Live data from Hacker News

Fogus: Node.js should become its own language

blog.fogus.me

11–20 of 79 posts

Re: Fogus: Node.js should become its own language

#12
post #9

"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

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

#13
post #4

Why 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.

It's not closures that I hope to avoid, but callbacks.

Re: Fogus: Node.js should become its own language

#14
post #8

"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.

You should also try CoffeeScript.

  dosomethingasync =>
      #etc
will automagically bind the closure to the current value of this.

Re: Fogus: Node.js should become its own language

#15
post #9

Earlier 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.

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...

Re: Fogus: Node.js should become its own language

#16
My greatest hope is that the makers of node.js will continue to abstain from language changes. Some inconvenient syntax is a small price to pay for the confidence that node.js isn't going to get turned into another regurgitation of Java meets Perl by hordes of perfectly well meaning but unsatisfiable programmers who want everything to work like the things they know. I don't mean to insult the makers or users of other languages, just to highlight the distinction of Javascript.

Re: Fogus: Node.js should become its own language

#17
Don't get me wrong, this could end up being the best thing since sliced bread, but the advantage of closures is that a higher-order approach can work wonders. Just take a look at async.js.

For 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).

> Javascript itself has very few faults

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

#19
post #15

Earlier 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...

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( function( ) { alert( i ) }, i * 250 ) } )( i )

Re: Fogus: Node.js should become its own language

#20
post #15

Earlier 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…

That was me who mentioned it, actually :-) Yes, I see what you mean now, sorry!

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.

Post reply on HN