views:

276

answers:

7

Can anyone point me in the direction of some really well written javascript examples. I have been trying to improve my javascript after reading a lot from douglas crockford book etc but i wondered where i could find some live examples of code that is written well?

+1  A: 

You may find these links useful:

Sarfraz
nice demos, but i think the OP is more about code quality than graphics.
galambalazs
@galambalazs: There is much more than graphics and it takes some great code to generate them. As for OP, only he can decide :)
Sarfraz
A: 

You could simply analyze the source of jquery. Although quite advanced, it's truly a bastion of quality, well documented code.

EDIT for future readers: as you can see from the comment feedback, the jquery source is not advisable for a beginner to analyze as it has some dirty hacks and is too hard to get in places. Avoid it and use some of the other suggestions instead.

Raveren
I have read though, that jquery isnt actually that well written
David
jQuery is written for terseness and with *many* ugly browser workarounds. It is absolutely not anything I would recommend new JS authors to try to learn from.
bobince
@David - I don't think you understand the context of *most* frameworks, not just jQuery, they take the browser inconsistencies out of the picture and try to add functionality *with* consistency. As @bobince mentions they're for a very specific purpose and try to be a light as possible. Whether it's well-written or not is another topic, but I do agree with @bobince, it's certainly not a good learning resource if you're starting out.
Nick Craver
I'm an experienced JavaScript developer, I've read through a fair bit of the jQuery source and I find it most of it hard to read and often unclear in intention, and therefore a terrible learning resource for anyone someone more inexperienced wanting to study and learn.
Tim Down
I don't think this answer should have a negative score, as I edited it to imply to NOT analyze jquery and that is something a beginner should know and pay attention to. Please upvote or remove one -1 so it has some credibility for future readers.
Raveren
@Raveren: No downvote from me.
Tim Down
A: 

some javascript framework like jQuery are well written and documented. Make sure you're downloading the development package (uncompressed one)

bangbambang
Disagree that jQuery source is a good learning tool. See @Raveren's answer.
Tim Down
as @Nick Craver comment on @Raveren's answer. jQuery isn't a good learning resources if you're starting up. My point is most of JS framework have good example of using Javascript functionality to deal with difference between browsers and make consistent output.
bangbambang
+1  A: 

I don't think there is 1 'standard' example, in the end everybody has their own style and approaches. Working in projects it's important to create/use good code guidelines/conventions.

An example article I found describing someones coding conventions based on the Yahoo! User Interface Library might explain what I'm talking about more.

As for right and wrong, there are a lot of ways to do things right. The importance is knowing what is wrong. And use coding conventions to make sure your code is consistent.

CharlesLeaf
Huh. I stopped reading this when he recommended using a `for`...`in` loop to iterate an `Array`. Not a good idea!
bobince
that's true, I hadn't actually read it myself... good for pointing that out. I just tried to find a quick -example- of coding conventions for my answer. I haven't seen any coding conventions online that I agree with 100%. I made my own.
CharlesLeaf
@bobince: Actually it doesn't recommend that, but it does seem to fundamentally misunderstand `for...in` loops, in that the example code seems to suggest that you get the property value rather than name in each iteration. Otherwise it looks like a straight regurgitation of Crockford's code standards, including the outright rejection of `new`, which doesn't sit well with me.
Tim Down
Ah, yes, I see... the term ‘Person array’ is probably just incidental sloppy English.
bobince
+2  A: 

Douglas Crockford is your man when it comes to some lean, mean, spanking javascript

His site has quite a few samples

Have a look here: http://javascript.crockford.com/code.html

and here: http://javascript.crockford.com/

Moin Zaman
No, most of his rules are stupid: 4 spaces, space between if and (), lines must have less than 80 characters, etc.
M28
they're only stupid if you follow all of them. use what you think works, throw the rest away. Could the other negative voter explain themself?
Moin Zaman
A: 

You should check out JSLint the JavaScript Quality tool. This is also a Douglas Crockford project.

You can paste in your code and get your verdict (scary), but you can also download the tool. I've integrated it into my test and builds so that all JavaScript is validated against JSLint before building and running unit tests (using Rhino for both). JSLint can be a bit tough on your code(ing style) - you can disable some of the check. I for instance I allow my code to use i++ Crockford does not.

I suggest that you read the other links provided by our fellow stackers so that you understand the workings of JSLint.

This project by legalbox - a scalable js app framework - is written using Crockford's teachings (amongst others). More over it is a pretty interesting project. Check out the code.

Michael
+2  A: 

Check out this project: Computer Science in JavaScript, by Nicholas C. Zakas

http://github.com/nzakas/computer-science-in-javascript/

tszming