The best JavaScript guide ever
41–50 of 56 posts
Re: The best JavaScript guide ever
#42While 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…
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
#43Earlier 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.
Re: The best JavaScript guide ever
#44I 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
#45Earlier 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.
Re: The best JavaScript guide ever
#46Sorry, I must have missed the download/offline option. Care to link me up?
Re: The best JavaScript guide ever
#47I 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
#48I 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.
Re: The best JavaScript guide ever
#49Earlier 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.
Re: The best JavaScript guide ever
#50I 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/
And with that, I close my thick O'Reilly book for the rest of the day.