It’s important to note that JSHint should be executed against your unit tests as well as your application code. The linter is your first line of defense against potential errors in your unit tests that may lead to false results.
Alternatives to JSHint
JSHint is not the only JavaScript linting tool; it just happens to be the tool that provides the functionality that we require. Here are some alternatives to JSHint that you might want to consider if you’re choosing a linting tool for your project.
JSLint
JSLint is a mature JavaScript linting tool that was created and is maintained by Douglas Crockford. Mr. Crockford is a key figure in the JavaScript
community, known for both his work on JSLint and his work to popularize JSON.
JSLint is available as an npm module for node.js, as well as a JavaScript file for use in browser-like environments.
The JSLint homepage can be found at http://www.jslint.com. ESLint
ESLint is a relatively new open-source linting tool created by Nicholas Zakas in 2013. Even though it is a young project, it is full-featured and very capable.
A feature unique to ESLint is its modularity. You may define your own custom linting rules and load them into the linter at run time. This is very useful if you would like to enforce a coding standard unique to your
organization.
ESLint is available as an npm module for node.js. The ESLint homepage can be found at http://www.eslint.org.
Strict Mode
One more code-checking tool merits attention. Actually, it’s not an external tool, but a JavaScript setting that was introduced with ECMAScript 5. If you include the following in a scope (either the global scope or a function), the JavaScript interpreter will process certain features differently.
'use strict';
With this directive in place, JavaScript will throw an error if you commit some common mistakes. These include using a variable without declaring it first, attempting to modify a read-only property, naming a variable with the reserved word arguments, and more.
When running on versions of JavaScript that do not support strict mode, the
'use strict' string will have no effect.
You will see an example of where strict mode can make a crucial difference in the “Default Binding and Strict Mode” section of Chapter 18. In the
meantime, the listings in this book will use it just to get in the habit.
SUMMARY
In this chapter, we introduced some of our favorite techniques and tools for JavaScript development.
A unit-testing framework is absolutely essential for reliable software
development. In this text, we will use Jasmine, which we have found easy to learn and very robust, but popular alternatives include QUnit and D.O.H.
As JavaScript applications become more complex, it becomes more important to keep their components clean and separate. Dependency injection is an important technique for doing just that. In this chapter, you worked through the test-driven development of a dependency-injection framework that will reappear in future chapters and may be useful in your own projects.
As another case study in TDD, you developed a toolkit for aspect-oriented programming. AOP allows you to enhance software components with
common functionality such as caching, without changing those components at all. It’s another way to simultaneously keep your code DRY, fulfill the Single Responsibility Principle, and adhere to the Open/Closed Principle.
Code-checking tools, known as linters, promote reliability at the micro level by alerting you to syntax mistakes or violations of standards. JSHint, JSLint, and ESLint are some of the most popular. Strict mode is another good way to avoid mistakes at the syntax level.
In the next chapter, you will explore the rich variety of ways in which
JavaScript can construct objects. It sounds like a simple topic, but you will see that it is anything but.
CHAPTER 3
Constructing Reliable Objects
WHAT’S IN THIS CHAPTER?
Creating data as primitives, object literals, and modules Creating data with the new keyword
Using methods of object creation to produce reliable objects
Controlling inheritance using prototypal, classical, and functional patterns Using monkey-patching responsibly
WROX.COM CODE DOWNLOADS FOR THIS CHAPTER The wrox.com code downloads for this chapter are found at
www.wrox.com/go/reliablejavascript on the Download Code tab.
Developers love dazzling effects in the user interface, elegant algorithms, and well-designed APIs, but often give little regard to the humble act of
instantiating an object. JavaScript offers an unusually varied assortment of choices for doing just that. Some of them will make your code reliable, testable, and extensible; others represent the easy road to perdition.
This chapter surveys the most common ways to create an object, considering how well each one meets the SOLID and DRY criteria for reliability and
testability introduced in Chapter 1.
Let’s start with the most basic and build our way up.