最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

formatting - How to write readable Javascript - Stack Overflow

programmeradmin2浏览0评论

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 ing 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?

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 ing 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?

Share Improve this question edited Dec 15, 2013 at 11:59 BenMorel 36.7k52 gold badges206 silver badges337 bronze badges asked Nov 2, 2011 at 20:39 JoeJoe 8,04218 gold badges57 silver badges86 bronze badges 3
  • 3 If I'm understanding your suggestion correctly it doesn't just make it harder to use closures, it makes it impossible. You should add some examples to show what you mean. Personally I don't have trouble reading Javascript which uses closures, but I have a few years under my belt. – coderjoe Commented Nov 2, 2011 at 20:42
  • 3 Not really. I write massive plex js sites and have no problems reading my own code or anyone else's so long as they indent as they would any other language. anonymous functions included. It's all up to you to decide to write in things like });}); instead of breaking them into their own lines and indent them properly. – Kai Qing Commented Nov 2, 2011 at 20:43
  • I find my JavaScript pretty readable, and I stick pretty much to typical formatting conventions with JavaScript. You do end up with });, but if you're nesting deeply enough that you can't read it later, then break it up as necessary: code turned on its side is not a graph of how awesome it is. But small, anonymous methods are canonical, and make sense most of the time. – Dave Newton Commented Nov 2, 2011 at 20:46
Add a ment  | 

3 Answers 3

Reset to default 5

I bet you should just follow already established coding standards.

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

  • Mootols (coding standard here), or
  • jQuery (coding standard here),

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 wele and should probably look like: ){), etc.

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

You need to format your code in order to be readable. And always while typing JS use IDE features to format code and each new mand put into new row. Then you won't have a problems.

You can use Online JavaScript beautifier for your existing javascript.

http://en.wikipedia/wiki/Indent_style http://en.wikipedia/wiki/Coding_conventions

Are a good place to start. I personally use the K&R style for clarity. When it es down to it though all you need to do is find a style you like and stick with it. Consistency ;]

发布评论

评论列表(0)

  1. 暂无评论