Coding patterns
Let's start with some patterns that reflect JavaScript's unique features. Some patterns aim to help you organize your code, for example, namespacing; others are related to improving performance, such as lazy definitions and init-time branching; and some make up for missing features, such as private properties. The patterns discussed in this section include the following topics:
- Separating behavior
- Namespaces
- Init-time branching
- Lazy definition
- Configuration objects
- Private variables and methods
- Privileged methods
- Private functions as public methods
- Immediate functions
- Chaining
- JSON
Separating behavior
As discussed previously, the three building blocks of a web page are as follows:
- Content (HTML)
- Presentation (CSS)
- Behavior (JavaScript)
Content
HTML is the content of the web page, the actual text. Ideally, the content should be marked-up using the least amount of HTML tags that sufficiently describe the semantic meaning of that content. For example, if you're working on a navigation...