Chapter 5. JavaScript Conventions
This chapter will cover some common conventions that you can use to improve your JavaScript. It covers topics such as making your code more readable by using comments and whitespace correctly, optimizing your code in order to improve performance, design patterns, and some common antipatterns (code that causes more problems than it solves).
Following the conventions in this chapter will significantly improve the performance and maintainability of your code, and you will find that both the quality of your code and your coding abilities will significantly improve.
Writing JavaScript
It is important when writing JavaScript to make your code as easy to read as possible. This makes your code easier to review, and easier to read back later (say, a couple of years down the road, when your project suddenly gains in popularity and a few bugs are found). It also means that if a developer wants to contribute to your project, or your project is passed on to another developer, that he can read and understand your code without having to study it for a long time beforehand.
Comments
By leaving comments in your code, you are ensuring that anyone who reads your code in the future will be able to tell what it does without having to dissect it. I generally leave comments before every function and at every line or block of code where it isn’t immediately clear what it does.
A good example would be this:
var
i
=
0
;
while
(
true
)
{
// Infinite loop until break is called
console ...
Get Learning from jQuery now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.