4 ms·
"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 t
by KeithMajhor 16y ago
"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).
- wladimir 16y agoMy 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.
- Cushman 16y agoYou should also try CoffeeScript. dosomethingasync => #etc will automagically bind the closure to the current value of this.
- demallien 16y ago'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.
- wladimir 16y agoThanks. 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.
- demallien 16y agoNo, 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...
- pygy_ 16y agoExcept 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
- udp 16y agoThey'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.
- deleted 16y ago[deleted]
- sedachv 16y ago"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.
- cyrus_ 16y agoSpidermonkey (or whatever they are calling it nowadays) supports this using the "let" keyword, fwiw.
- KeithMajhor 16y agoTry 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.
- udp 16y agoThat'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...
- swannodette 16y ago> 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.
- fogus 16y agoJavascript itself has very few faults. We disagree. Signed, Douglas Crockford, Jeremy Ashkenas, and Brendan Eich
- KeithMajhor 16y agoCould you elaborate?
- dminor 16y agoCrockford wrote a book titled "JavaScript: The Good Parts", which more or less implies there are bad parts as well.
- dionidium 16y agoI think maybe you mean to say that you wish JavaScript had block scope. Lexical scope, it has.