News for hackers. Stuff that matters.
Re:The best JavaScript guide ever by Samuel_Michon on Wednesday October 06, 2010 @11:14AM (#1764254)
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/

Re:The best JavaScript guide ever by ktf on Wednesday October 06, 2010 @01:15PM (#1764830)
Eloquent JavaScript is being being published in print form soon as well: http://nostarch.com/ejs.htm
Re:The best JavaScript guide ever by marijn on Thursday October 07, 2010 @05:32AM (#1767301)
Thanks for the promotion. Made my morning.
Re:The best JavaScript guide ever by mhansen on Thursday October 07, 2010 @05:39AM (#1767315)
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 by mhb on Wednesday October 06, 2010 @11:43AM (#1764400)
And Marijn developed Postmodern for Lisp Postgres access too!
Re:The best JavaScript guide ever by macco on Wednesday October 06, 2010 @05:54PM (#1766019)
Yeah you are right. Eloquent Javascript is the best introduction I know - free or commercial. Most Javascript books I know aren't very good.
Re:The best JavaScript guide ever by jinfiesto on Wednesday October 06, 2010 @11:20AM (#1764275)
I agree. Eloquent Javascript is excellent.
Re:The best JavaScript guide ever by lkrubner on Wednesday October 06, 2010 @11:26AM (#1764311)
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 by Samuel_Michon on Wednesday October 06, 2010 @11:30AM (#1764330)
I just checked in Chrome 6 on Mac OS X 10.6.4 -- works fine here. In Safari 5 Mac as well.
Re:The best JavaScript guide ever by marijn on Thursday October 07, 2010 @05:26AM (#1767292)
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 by 20after4 on Thursday October 07, 2010 @06:08AM (#1767356)
works for me
Re:The best JavaScript guide ever by stuff4ben on Thursday October 07, 2010 @09:58AM (#1767820)
Works for me in Chrome 6.0.472.63 on MacOS 10.6.4.
Re:The best JavaScript guide ever by bl4k on Thursday October 07, 2010 @04:41AM (#1767229)
That book has an excellent chapter on object oriented Javascript, which is sorely lacking in most js books.
Re:The best JavaScript guide ever by thisisblurry on Thursday October 07, 2010 @08:19AM (#1767568)
Wow, that is a great find.

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

Re:The best JavaScript guide ever by kreek on Wednesday October 06, 2010 @11:04AM (#1764192)
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

Re:The best JavaScript guide ever by kenjackson on Wednesday October 06, 2010 @11:37AM (#1764374)
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 by sysprv on Wednesday October 06, 2010 @12:52PM (#1764742)
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 by kraynar on Wednesday October 06, 2010 @02:44PM (#1765255)
Agreed, but the name is Crockford. His site, which has a bunch of interesting JavaScript links on it: http://crockford.com
Re:The best JavaScript guide ever by kreek on Wednesday October 06, 2010 @03:36PM (#1765509)
Woops my bad, apologies to Mr. Crockford.
Re:The best JavaScript guide ever by thisisblurry on Thursday October 07, 2010 @08:22AM (#1767577)
Re:The best JavaScript guide ever by Groxx on Wednesday October 06, 2010 @02:11PM (#1765083)
I wish more books were like that. A programming book for programmers.
Re:The best JavaScript guide ever by code_duck on Wednesday October 06, 2010 @06:52PM (#1766178)
His point that JavaScript is quite good with a few key flaws, and the reason so many people think they hate JS is the flaws of the DOM, is an important observation.
Re:The best JavaScript guide ever by revjx on Wednesday October 06, 2010 @11:01AM (#1764174)
Is this the same Mozilla JS reference that's been kicking around for ages? I know there seems to be a big campaign to promote JS resources at the moment, but surely everyone on HN would know about this if they have more than a passing interest in Javascript or learning it...
Re:The best JavaScript guide ever by adamhowell on Wednesday October 06, 2010 @11:21AM (#1764284)
It's on the frontpage of Reddit at the moment, so I assume that's why it's here, too.
Re:The best JavaScript guide ever by wazoox on Wednesday October 06, 2010 @10:32AM (#1764041)
It is not a tutorial. It's a reference guide.
Re:The best JavaScript guide ever by dolinsky on Wednesday October 06, 2010 @11:06AM (#1764204)
...and yet it has 17 points only being posted 16 minutes ago. Is PG working at all on an algorithm that takes into consideration factors such as the repetition with which certain accounts vote up a link (or a group of accounts) and inversely weigh that against other links sumbitted? Possibly implementing the results via the Fisher Method / Bayesian rank of some kind? As the noise to signal ratio has widened with HN's popularity I find it harder to see the 'good stuff' bubble to the top.
Re:The best JavaScript guide ever by dolinsky on Wednesday October 06, 2010 @11:53AM (#1764444)
Why am I not surprised I'm being downvoted. Links like this, and the response they get (46 votes now) take away valuable space from fresh, timely and relevant articles yet HN can just as easily be gamed as Digg/Reddit to allow this to happen.
Re:The best JavaScript guide ever by alrex021 on Wednesday October 06, 2010 @10:59AM (#1764167)
Good point. I'll update the title.

[edit] title updated

Re:The best JavaScript guide ever by aw3c2 on Wednesday October 06, 2010 @11:08AM (#1764222)
Please read http://ycombinator.com/newsguidelines.html and edit accordingly. Something like "The Mozilla Developer Center's JavaScript Guide" would be a good title.
Re:The best JavaScript guide ever by gmjosack on Wednesday October 06, 2010 @12:42PM (#1764678)
The linkbait title was ripped straight from reddit where it was also a bad title.
Re:The best JavaScript guide ever by trustfundbaby on Wednesday October 06, 2010 @12:07PM (#1764515)
I think the title is fine as it is now ... makes it clear that this isn't just another JavaScript guide/tutorial.
Re:The best JavaScript guide ever by city41 on Wednesday October 06, 2010 @12:20PM (#1764568)
I disagree. The title is against HN's posting guidelines. It's also not unanimous, as even in the comments here people have suggested other ways to learn JS that they feel are superior.

Noting in the title that this is Mozilla's JS reference is enough to give it credence and is still objective.

Re:The best JavaScript guide ever by alexyim on Wednesday October 06, 2010 @02:20PM (#1765125)
I like John Resig's tutorial: http://ejohn.org/apps/learn/
Re:The best JavaScript guide ever by thisisblurry on Thursday October 07, 2010 @08:24AM (#1767581)
I used Resig's a few days ago to quickly fix a bug at work. He really does a great job of making very complex language constructs understandable and easy to follow.
Re:The best JavaScript guide ever by ez77 on Wednesday October 06, 2010 @03:28PM (#1765473)
A bit off topic: does anybody know why Mozilla uses https for this kind of open stuff?
Re:The best JavaScript guide ever by eel on Thursday October 07, 2010 @12:48PM (#1768421)
I do like the content of MDC, but it can be frustrating at times, as it seems to have broken links (links that go to pages which have had their content merged into a different page).

Or apparently, on the Processing XML with E4X page, there are errors in the page: /content/body/div[2]/pre[1]/@function, reference to undefined name 'syntax': line 1, column 1 where every code sample should be.

It's under a CC license, so I am hoping that someone will make a better interface for the content some day.

Re:The best JavaScript guide ever by etherael on Wednesday October 06, 2010 @07:23PM (#1766262)
This used to be my sole resource for everything javascript, it is actually very good still but there's a slight catch; beware cross browser compatibility issues, especially with IE, especially especially with older versions of IE.
Re:The best JavaScript guide ever by pyre on Wednesday October 06, 2010 @06:32PM (#1766125)
Sorry, I must have missed the download/offline option. Care to link me up?
Re:The best JavaScript guide ever by marijn on Thursday October 07, 2010 @05:31AM (#1767300)
Re:The best JavaScript guide ever by Tichy on Wednesday October 06, 2010 @11:52AM (#1764440)
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 be a bug in the interpreter? Or when is something an object, and when just a block? var a = x:1 doesn't work, btw. this.x is also still undefined after this.

The only way I could return my wanted object was by doing var y = {x:1};y;

(This was supposed to be the return value of a Script in NodeJS, using these silly values because it was a unit test).

Re:The best JavaScript guide ever by points on Wednesday October 06, 2010 @12:07PM (#1764523)

    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, "foo:" = label.

  Console>({foo:1})
  {foo:1}
Return an object:

  function foo() {
    return {bar:23, baz: function() {return 89}};
  }
Re:The best JavaScript guide ever by Tichy on Wednesday October 06, 2010 @01:25PM (#1764872)
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 by points on Wednesday October 06, 2010 @01:48PM (#1764965)
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 by tlrobinson on Wednesday October 06, 2010 @02:19PM (#1765124)

    eval("{x:1}") // returns 1
    eval("({x:1})") // returns {x:1}
Re:The best JavaScript guide ever by nfriedly on Wednesday October 06, 2010 @12:04PM (#1764501)
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 idea to avoid them.

Relatedly, to "fake" a block in JS, wrap it in a function:

  (function(){
    var a = 1;
    // do something with a
  }());
  
  // out side of the "block" a is undefined (or whatever value it was before the "block")
[Edited for accuracy and clarity]
Re:The best JavaScript guide ever by tlrobinson on Wednesday October 06, 2010 @02:01PM (#1765031)
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")
Re:The best JavaScript guide ever by points on Wednesday October 06, 2010 @01:50PM (#1764979)
> "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 by nfriedly on Wednesday October 06, 2010 @08:28PM (#1766405)
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 by Tichy on Thursday October 07, 2010 @03:11AM (#1767085)
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 by nfriedly on Thursday October 07, 2010 @09:40AM (#1767763)
Gotcha, glad you've got it figured out then =]
Re:The best JavaScript guide ever by Tichy on Wednesday October 06, 2010 @01:23PM (#1764864)
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 by pdelgallego on Wednesday October 06, 2010 @01:42PM (#1764933)
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 by wtn on Wednesday October 06, 2010 @07:38PM (#1766290)
Would be nice if they allowed you to download a tarball of the whole guide.