Earlier quoted context omitted.
Good point. I'll update the title. [edit] title updated
Please read http://ycombinator.com/newsguidelines.html and edit accordingly. Something like "The Mozilla Developer Center's JavaScript Guide" would be a good title.
The best JavaScript guide ever
21–30 of 56 posts
Re: The best JavaScript guide ever
#22The best way I've found to learn JavaScript is through anything by Douglas Crawford. 'JavaScript the Good Parts' is especially good. http://oreilly.com/catalog/9780596517748 http://www.youtube.com/watch?v=hQVTIJBZook
I was a Javascript hater for a long time. Douglas turned me around through his videos and writing. Still not my favorite language, but I have a lot more respect for it and actually wouldn't mind writing in it if needed. I also strongly recommend his work for a better understanding of Javascript.
Re: The best JavaScript guide ever
#23I personally prefer programming guides with some humor, lots of examples and exercises. That's why I recommend Eloquent Javascript by Marijn Haverbeke. http://eloquentjavascript.net/
Re: The best JavaScript guide ever
#24While we are at it, perhaps somebody could explain this JS gotcha I recently ran into? My goal was to return something like {x:1} (an object) from a script. It never worked. If I type that into a console, it returns "1". If I type x:1 into the console, it also returns 1. So I guess the "{}" is just interpreted as a block, returning the value of the x:1 expression. But what does x:1 mean? I am guessing it might just b…
JavaScript doesn't have blocks. {} is shorthand notation for an object, so you would want something like function asdf(){ return {x:1}; } It's a less verbose way of doing this: function asdf2(){ var myObj = new Object(); myObj.x = 1; return myObj; } Outside of a object written in shorthand notation, x:1 is treated as a label (x:) followed by a value (1). In general labels are not used in JS, and it's probably a good…
I have actually used labels in JS before, which resulted in a lot of questions from my colleagues.
I think the loop: for(...){break loop;} thing is acceptable (if for can not be avoided anyway).
Re: The best JavaScript guide ever
#25While we are at it, perhaps somebody could explain this JS gotcha I recently ran into? My goal was to return something like {x:1} (an object) from a script. It never worked. If I type that into a console, it returns "1". If I type x:1 into the console, it also returns 1. So I guess the "{}" is just interpreted as a block, returning the value of the x:1 expression. But what does x:1 mean? I am guessing it might just b…
var a = x:1 This is invalid. Stop guessing. Read the language spec. Best by far is "Definitive Javascript O'Reilly" {} is a block of statements. For example, {doSomething();doAnotherThing()} {} when used as an expression, is object literal notation. For example, var a = {foo:23,bar:99}; If you want to be explicit that something is an expression, enclose it in parenthesis... Console>{foo:1} 1 // "{}" = code block, "fo…
I like the ({x:1}) shortcut, thanks!
Re: The best JavaScript guide ever
#26While we are at it, perhaps somebody could explain this JS gotcha I recently ran into? My goal was to return something like {x:1} (an object) from a script. It never worked. If I type that into a console, it returns "1". If I type x:1 into the console, it also returns 1. So I guess the "{}" is just interpreted as a block, returning the value of the x:1 expression. But what does x:1 mean? I am guessing it might just b…
==== if (true) { function foo(){ return 1; } } else { function foo(){ return 2; } } foo();
firefox return 1 webkit return 2 Other engines return an error (that is what is suppose to happen according to the specification)
Re: The best JavaScript guide ever
#27Earlier quoted context omitted.
var a = x:1 This is invalid. Stop guessing. Read the language spec. Best by far is "Definitive Javascript O'Reilly" {} is a block of statements. For example, {doSomething();doAnotherThing()} {} when used as an expression, is object literal notation. For example, var a = {foo:23,bar:99}; If you want to be explicit that something is an expression, enclose it in parenthesis... Console>{foo:1} 1 // "{}" = code block, "fo…
Returning an object from a function was not the problem - it was "returning" from a Script (as executed by the NodeJS Script class). So essentially the last value in the script, I suppose. I like the ({x:1}) shortcut, thanks!
doSomething();
({x:1});Re: The best JavaScript guide ever
#28While we are at it, perhaps somebody could explain this JS gotcha I recently ran into? My goal was to return something like {x:1} (an object) from a script. It never worked. If I type that into a console, it returns "1". If I type x:1 into the console, it also returns 1. So I guess the "{}" is just interpreted as a block, returning the value of the x:1 expression. But what does x:1 mean? I am guessing it might just b…
Re: The best JavaScript guide ever
#29While we are at it, perhaps somebody could explain this JS gotcha I recently ran into? My goal was to return something like {x:1} (an object) from a script. It never worked. If I type that into a console, it returns "1". If I type x:1 into the console, it also returns 1. So I guess the "{}" is just interpreted as a block, returning the value of the x:1 expression. But what does x:1 mean? I am guessing it might just b…
JavaScript doesn't have blocks. {} is shorthand notation for an object, so you would want something like function asdf(){ return {x:1}; } It's a less verbose way of doing this: function asdf2(){ var myObj = new Object(); myObj.x = 1; return myObj; } Outside of a object written in shorthand notation, x:1 is treated as a label (x:) followed by a value (1). In general labels are not used in JS, and it's probably a good…
I think what you mean to say is that Javascript doesn't have block scope for variables. It has blocks.
Re: The best JavaScript guide ever
#30While we are at it, perhaps somebody could explain this JS gotcha I recently ran into? My goal was to return something like {x:1} (an object) from a script. It never worked. If I type that into a console, it returns "1". If I type x:1 into the console, it also returns 1. So I guess the "{}" is just interpreted as a block, returning the value of the x:1 expression. But what does x:1 mean? I am guessing it might just b…
JavaScript doesn't have blocks. {} is shorthand notation for an object, so you would want something like function asdf(){ return {x:1}; } It's a less verbose way of doing this: function asdf2(){ var myObj = new Object(); myObj.x = 1; return myObj; } Outside of a object written in shorthand notation, x:1 is treated as a label (x:) followed by a value (1). In general labels are not used in JS, and it's probably a good…
if (...) {
// block
}
It turns out that blocks don't need to follow a control flow construct like "if", "while", etc, they can stand on their own anywhere a statement can.What's happening in the above case is the syntax for an object literal and a block is ambiguous. It's normally not a problem since you don't have object literals standing on their own as statements, you do something with them as an expression. On a console (or any time you eval an expression) you should wrap it in a parenthesis to disambiguate it:
({x:1}) // expression (object with "x" property equal to "1")
{x:1} // statement (label "x" followed by statement "1")