Live data from Hacker News

Ask HN: Javascript best practices?

news.ycombinator.com

1–10 of 52 posts

Ask HN: Javascript best practices?

#1
How much does it make sense to decompose your javascript routines into separate files? Do you have .js for related classes and methods and one page wide .js to call everything else?

EDIT: The above was just an example, I'm looking for most things related to JS.

Re: Ask HN: Javascript best practices?

#3
Many frameworks have some tools for dealing with multiple javascript files. I use django-compress for example and love it.

It allows you to have many different javascript files to separate your code logically, and it automatically compresses them into one file for you for deployment. In this way you don't have to compromise speed for clarity.

If you're interested in advanced JS I highly recommend this book. http://jspro.org/

Re: Ask HN: Javascript best practices?

#4

Many frameworks have some tools for dealing with multiple javascript files. I use django-compress for example and love it. It allows you to have many different javascript files to separate your code logically, and it automatically compresses them into one file for you for deployment. In this way you don't have to compromise speed for clarity. If you're interested in advanced JS I highly recommend this book. http://js…

I have been eyeing books for months now. Pro Javascript Techniques has been one of them. Living in a country where shipping costs as much as a book is hard.

Re: Ask HN: Javascript best practices?

#5
I'll assume this is for client side js, since that's what most people use =)

Most of the js I write involves jQuery. As such, I find it's very useful to separate reusable components into jQuery plugins in separate files. If you don't use jQuery or another library which allows plugins, then it's best to create your own namespaced module.

See the "Module pattern" for more info: http://www.yuiblog.com/blog/2007/06/12/module-pattern/

Depending on the complexity of your code, you might want to look into javascript build systems -- there's at least one for every major web framework.

Here's some advantages they offer: - Allow bunding of many js files into one mega js file. (reduces HTTP requests for faster page loading) - Specify javascript dependencies using special syntax. (easier maintenance) - Versioning support (so users don't get an old cached version of your code)

When bundling js files, I try to aim for a maximum of two js files per page. One contains code which is global to the whole site (this is cached on subsequent views), and the second which contains page specific code.

Re: Ask HN: Javascript best practices?

#6
post #4

Many frameworks have some tools for dealing with multiple javascript files. I use django-compress for example and love it. It allows you to have many different javascript files to separate your code logically, and it automatically compresses them into one file for you for deployment. In this way you don't have to compromise speed for clarity. If you're interested in advanced JS I highly recommend this book. http://js…

I have been eyeing books for months now. Pro Javascript Techniques has been one of them. Living in a country where shipping costs as much as a book is hard.

Living in a country where shipping costs as much as a book is hard.

Might Safari Books Online work for you?

http://www.safaribooksonline.com/

I prefer the physical book to reading from a browser, but if the price is right...

Re: Ask HN: Javascript best practices?

#7

Many frameworks have some tools for dealing with multiple javascript files. I use django-compress for example and love it. It allows you to have many different javascript files to separate your code logically, and it automatically compresses them into one file for you for deployment. In this way you don't have to compromise speed for clarity. If you're interested in advanced JS I highly recommend this book. http://js…

Thanks, I totally forgot about the Resig book since it was announced. Just bought.

I really liked _Javascript: The Good Parts_:

http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockfor...

Particularly because it's purely about style and code organization, and not about the best way to get consistent DOM access between Gecko, WebKit, and IE.

Re: Ask HN: Javascript best practices?

#8
Just remember that sometimes doing things the "right" way can really slow your code down. I started coding believing that text should be added with createTextNode and any HTML element should be created via createElement.

This causes two issues:

1. Memory leaks from deleted or hidden DOM that is still referenced.

2. Performance. innerHTML is still probably the fastest method of adding elements, as dirty as it seems.

Re: Ask HN: Javascript best practices?

#10

I like to use separate files for organization during development and then merge and minify for deployment. Just make sure you have an automated process because you don't want to make mistakes doing it by hand.

Any tool suggestions for the job of minifying and deployment?
Post reply on HN