Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Maintainable JavaScript
Introduction
Preface
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
I. Style Guidelines
1. Basic Formatting
Indentation Levels
Statement Termination
Line Length
Line Breaking
Blank Lines
Naming
Variables and Functions
Constants
Constructors
Literal Values
Strings
Numbers
Null
Undefined
Object Literals
Array Literals
2. Comments
Single-Line Comments
Multiline Comments
Using Comments
Difficult-to-Understand Code
Potential Author Errors
Browser-Specific Hacks
Documentation Comments
3. Statements and Expressions
Brace Alignment
Block Statement Spacing
The switch Statement
Indentation
Falling Through
default
The with Statement
The for Loop
The for-in Loop
4. Variables, Functions, and Operators
Variable Declarations
Function Declarations
Function Call Spacing
Immediate Function Invocation
Strict Mode
Equality
eval()
Primitive Wrapper Types
II. Programming Practices
5. Loose Coupling of UI Layers
What Is Loose Coupling?
Keep JavaScript Out of CSS
Keep CSS Out of JavaScript
Keep JavaScript Out of HTML
Keep HTML Out of JavaScript
Alternative #1: Load from the Server
Alternative #2: Simple Client-Side Templates
Alternative #3: Complex Client-Side Templates
6. Avoid Globals
The Problems with Globals
Naming Collisions
Code Fragility
Difficulty Testing
Accidental Globals
Avoiding Accidental Globals
The One-Global Approach
Namespaces
Modules
YUI modules
Asynchronous Module Definition (AMD) Modules
The Zero-Global Approach
7. Event Handling
Classic Usage
Rule #1: Separate Application Logic
Rule #2: Don’t Pass the Event Object Around
8. Avoid Null Comparisons
Detecting Primitive Values
Detecting Reference Values
Detecting Functions
Detecting Arrays
Detecting Properties
9. Separate Configuration Data from Code
What Is Configuration Data?
Externalizing Configuration Data
Storing Configuration Data
10. Throw Your Own Errors
The Nature of Errors
Throwing Errors in JavaScript
Advantages of Throwing Errors
When to Throw Errors
The try-catch Statement
Throw or try-catch?
Error Types
11. Don’t Modify Objects You Don’t Own
What Do You Own?
The Rules
Don’t Override Methods
Don’t Add New Methods
Don’t Remove Methods
Better Approaches
Object-Based Inheritance
Type-Based Inheritance
The Facade Pattern
A Note on Polyfills
Preventing Modification
12. Browser Detection
User-Agent Detection
Feature Detection
Avoid Feature Inference
Avoid Browser Inference
What Should You Use?
III. Automation
13. File and Directory Structure
Best Practices
Basic Layout
14. Ant
Installation
The Build File
Running the Build
Target Dependencies
Properties
Buildr
15. Validation
Finding Files
The Task
Improving the Target
Other Improvements
Buildr Task
16. Concatenation and Baking
The Task
Line Endings
Headers and Footers
Baking Files
17. Minification and Compression
Minification
Minifying with YUI Compressor
Minifying with Closure Compiler
Minifying with UglifyJS
Compression
Runtime Compression
Build-Time Compression
18. Documentation
JSDoc Toolkit
YUI Doc
19. Automated Testing
YUI Test Selenium Driver
Setting Up a Selenium Server
Setting Up YUI Test Selenium Driver
Using the YUI Test Selenium Driver
The Ant Target
Yeti
PhantomJS
Installation and Usage
The Ant Target
JsTestDriver
Installation and Usage
The Ant Target
20. Putting It Together
Missing Pieces
Planning the Build
The Development Build
The Integration Build
The Release Build
Using a CI System
Jenkins
Other CI Systems
A. JavaScript Style Guide
Indentation
Line Length
Primitive Literals
Operator Spacing
Parentheses Spacing
Object Literals
Comments
Single-Line Comments
Multiline Comments
Comment Annotations
Variable Declarations
Function Declarations
Naming
Strict Mode
Assignments
Equality Operators
Ternary Operator
Statements
Simple Statements
return Statement
Compound Statements
if Statement
for Statement
while Statement
do Statement
switch Statement
try Statement
White Space
Things to Avoid
B. JavaScript Tools
Build Tools
Documentation Generators
Linting Tools
Minification Tools
Testing Tools
Index
About the Author
Colophon
← Prev
Back
Next →
← Prev
Back
Next →