Search code examples
javascriptformattingreadability

How to write readable Javascript


In JavaScript, the standard rules for code formatting don't seem to cut it. You still end up with messes of });}); all over the place and I don't believe I even know of established rules for the correct indention of anonymous functions declared as arguments to other functions.

In short, I have trouble reading my own JavaScript, and I bet I'm not alone.

I think the idea that I am coming around to is to just not define functions within other functions. Write every function with a name and as a child to the script tag. When you need to pass functions around, use their names. Of course, this makes it a lot harder to use closures [edit: you cannot use closures at all if you write your code that way]. Also, this might cause problems with memory management?

Anyone else have thoughts on this?


Solution

  • I bet you should just follow already established coding standards.

    You may follow coding standard for one of two best JavaScript frameworks:

    Basically use tabs instead of spaces (this may be difficult for some, but I believe is established standard for JS), avoid unnecessary spaces (eg. in ) { in function definition space is not welcome and should probably look like: ){), etc.

    PS. It is my personal > opinion < that MooTools & jQuery are 2 the best JS frameworks.