News for hackers. Stuff that matters.
Re:Fogus: Node.js should become its own language by KeithMajhor on Tuesday April 12, 2011 @09:34AM (#2437088)
"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).

Re:Fogus: Node.js should become its own language by swannodette on Tuesday April 12, 2011 @10:01AM (#2437198)
> 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 by fogus on Tuesday April 12, 2011 @11:10AM (#2437509)

    Javascript itself has very few faults.
We disagree.

Signed,

Douglas Crockford, Jeremy Ashkenas, and Brendan Eich

Re:Fogus: Node.js should become its own language by KeithMajhor on Tuesday April 12, 2011 @11:33AM (#2437616)
Could you elaborate?
Re:Fogus: Node.js should become its own language by dminor on Tuesday April 12, 2011 @11:44AM (#2437683)
Crockford wrote a book titled "JavaScript: The Good Parts", which more or less implies there are bad parts as well.
Re:Fogus: Node.js should become its own language by pygy_ on Tuesday April 12, 2011 @09:49AM (#2437144)
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 by udp on Tuesday April 12, 2011 @09:52AM (#2437158)
They're "hoisted" and only local to the function, though. I'd (personally) prefer it if it worked like C, and a variable was defined from the "var" statement until the next closing brace. Then you could have a variable local to one branch of an if statement, etc, without faking scopes by abusing closures (eg (function() { })(); ).

And nested loops that both use "i" would work.

Re:Fogus: Node.js should become its own language by sedachv on Tuesday April 12, 2011 @11:38AM (#2437647)
"I'd (personally) prefer it if it worked like C, and a variable was defined from the "var" statement until the next closing brace."

It does work like C. The scoping you're talking about was introduced in C99.

Re:Fogus: Node.js should become its own language by cyrus_ on Tuesday April 12, 2011 @09:32PM (#2440298)
Spidermonkey (or whatever they are calling it nowadays) supports this using the "let" keyword, fwiw.
Re:Fogus: Node.js should become its own language by KeithMajhor on Tuesday April 12, 2011 @09:53AM (#2437165)
Try this: for ( var i = 1; i < 10; ++i ) setTimeout( function( ) { alert( i ) }, i * 250 )

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 by udp on Tuesday April 12, 2011 @09:56AM (#2437179)
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 by KeithMajhor on Tuesday April 12, 2011 @10:02AM (#2437202)
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 by raganwald on Tuesday April 12, 2011 @10:12AM (#2437247)
> You can simulate the behavior of lexical scoping by "abusing" closures as someone mentioned before.

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!

Re:Fogus: Node.js should become its own language by Cushman on Tuesday April 12, 2011 @10:13AM (#2437255)
For the record, you can also create scope without a closure using with (or let, if it's available.)

  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.
Re:Fogus: Node.js should become its own language by brlewis on Tuesday April 12, 2011 @10:35AM (#2437360)
That isn't lack of lexical scope. It's that the for() syntax isn't purely functional in how it binds variables. One could define an equivalent syntax in Scheme and see the same result.

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))))
Re:Fogus: Node.js should become its own language by udp on Tuesday April 12, 2011 @10:04AM (#2437214)
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.

Re:Fogus: Node.js should become its own language by KeithMajhor on Friday April 15, 2011 @03:15PM (#2451769)
This is completely wrong. I don't know what I was thinking.

It would be the same variable with or without lexical scope (by that I mean block-level).

Re:Fogus: Node.js should become its own language by dkersten on Tuesday April 12, 2011 @11:46AM (#2437698)
Python does this as well.

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.

Re:Fogus: Node.js should become its own language by wladimir on Tuesday April 12, 2011 @09:48AM (#2437139)
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.
Re:Fogus: Node.js should become its own language by Cushman on Tuesday April 12, 2011 @09:56AM (#2437178)
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 by demallien on Tuesday April 12, 2011 @11:11AM (#2437516)
'this' is evil in Javascript. The very first line in any constructor function that I write is: var that = 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.

Re:Fogus: Node.js should become its own language by wladimir on Tuesday April 12, 2011 @11:51AM (#2437716)
Thanks. I think I like that solution, as it's extremely simple. Are there any drawbacks?

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.

Re:Fogus: Node.js should become its own language by demallien on Tuesday April 12, 2011 @12:01PM (#2437771)
No, no drawbacks that I'm aware of. You're basically using a closure to bind the object to the callback, instead of using Javascript's weird 'this' binding rules, that's all...
Re:Fogus: Node.js should become its own language by dionidium on Tuesday April 12, 2011 @11:37AM (#2437639)
I think maybe you mean to say that you wish JavaScript had block scope. Lexical scope, it has.