Live data from Hacker News

The best JavaScript guide ever

developer.mozilla.org

21–30 of 56 posts

Re: The best JavaScript guide ever

#21
post #7
post #3

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 linkbait title was ripped straight from reddit where it was also a bad title.

Re: The best JavaScript guide ever

#22
post #5

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

Met Douglas at JAOO (http://gotocon.com/) this year; got him to sign my copy of "JavaScript: The Good Parts" :)

Re: The best JavaScript guide ever

#23

I 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/

Eloquent JavaScript is being being published in print form soon as well: http://nostarch.com/ejs.htm

Re: The best JavaScript guide ever

#24
post #15

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

Of course, a label! That explains it, thanks! I didn't have a JS reference handy at the time (and couldn't find it in the Mozilla ref in the short amount of time I had).

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

#25
post #19
post #15

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

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!

Re: The best JavaScript guide ever

#26
post #15

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

This works different is firefox than in webkit.

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

#27
post #25
post #19

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

I don't quite understand what you mean by 'returning from a script'. That doesn't make too much sense within Javascript itself, if it's a case of the last expression being considered the return value (I haven't used NodeJS), then you would want:

  doSomething();
  ({x:1});

Re: The best JavaScript guide ever

#28
post #15

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

[deleted]

Re: The best JavaScript guide ever

#29
post #15

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

> "JavaScript doesn't have blocks"

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

#30
post #15

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

More precisely, JavaScript doesn't have block scope. It does have syntax for grouping multiple statements into "blocks" (not sure if that's the right word), e.x.

    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")
Post reply on HN