Live data from Hacker News

The best JavaScript guide ever

developer.mozilla.org

41–50 of 56 posts

Re: The best JavaScript guide ever

#42
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…

Yes, as several people have mentioned, I should have said block scope. JS does allow for blocks, but they don't work the way the parent was thinking.

You can toss extra curly braces in the code and it is a "block" but unless they are preceded by an "if", "for", or something similar, they don't do much. They do not return values and they do not affect variable scope. Functions do both, hence my last example.

Re: The best JavaScript guide ever

#43

Earlier quoted context omitted.

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…

Yes, as several people have mentioned, I should have said block scope . JS does allow for blocks, but they don't work the way the parent was thinking. You can toss extra curly braces in the code and it is a "block" but unless they are preceded by an "if", "for", or something similar, they don't do much. They do not return values and they do not affect variable scope. Functions do both, hence my last example.

I think my understanding of blocks is fine. "Returning" was the wrong expression, I meant the value of a script (like the result of an eval). I am aware of the lack of block scope in JS :-)

Re: The best JavaScript guide ever

#44

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/

That book has an excellent chapter on object oriented Javascript, which is sorely lacking in most js books.

Re: The best JavaScript guide ever

#45

Earlier quoted context omitted.

I agree. Eloquent Javascript is excellent.

At least in Google Chrome on a Mac, all I get are blank white pages. If I hit view source, then I see text, but otherwise I see no text.

Can you tell me which version you're using? I'm also not seeing any problems (in version 6), but I'd like to figure out what's happening for you.

Re: The best JavaScript guide ever

#48
post #47

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/

Thanks for the promotion. Made my morning.

Marijn, thanks for writing this. I'm a big fan - I recommend it to my friends looking to learn JavaScript.

Re: The best JavaScript guide ever

#49
post #45

Earlier quoted context omitted.

At least in Google Chrome on a Mac, all I get are blank white pages. If I hit view source, then I see text, but otherwise I see no text.

Can you tell me which version you're using? I'm also not seeing any problems (in version 6), but I'd like to figure out what's happening for you.

works for me

Re: The best JavaScript guide ever

#50

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/

Wow, that is a great find.

And with that, I close my thick O'Reilly book for the rest of the day.

Post reply on HN