TIENE EN SU CESTA DE LA COMPRA
en total 0,00 €
Create more robust applications with a test-first approach to JavaScript
Reliable JavaScript, How to Code Safely in the World´s Most Dangerous Language demonstrates how to create test-driven development for large-scale JavaScript applications that will stand the test of time and stay accurate through long-term use and maintenance. Taking a test-first approach to software architecture, this book walks you through several patterns and practices and explains what they are supposed to do by having you write unit tests. Write the code to pass the unit tests, so you not only develop your technique for structuring large-scale applications, but you also learn how to test your work. You´ll come away with hands-on practice that results in code that is correct from the start, and has the test coverage to ensure that it stays correct during subsequent maintenance. All code is provided both in the text and on the web, so you can immediately get started designing more complete, robust applications.
JavaScript has graduated from field-validation scripts to full-scale applications, but many developers still approach their work as if they were writing simple scripts. If you´re one of those developers, this book is the solution you need to whip your code into shape and create JavaScript applications that work.
Write more concise and elegant code by thinking in JavaScript
Test the implementation and use of common design patterns
Master the use of advanced JavaScript features
Ensure your code´s conformance to your organization´s standards
If you´re ready to step up your code and develop more complete software solutions, Reliable JavaScript is your essential resource.
Table of Contents
INTRODUCTION xxi
PART I: LAYING A SOLID FOUNDATION
CHAPTER 1: PRACTICING SKILLFUL SOFTWARE 3
Writing Code That Starts Correct 4
Mastering the Features of JavaScript 4
Case Study: D3.js 5
JavaScript Is Single-Threaded 15
Avoiding JavaScript's Pitfalls in Larger Systems 16
Scripts Are Not Modules 16
Nested Functions Control Scope 16
Coding by Contract 17
Applying the Principles of Software Engineering 18
The SOLID Principles 18
The DRY Principle 21
Writing Code That Stays Correct 22
Investing for the Future with Unit Tests 22
Practicing Test-Driven Development 23
Engineering Your Code to Be Easy to Test 24
Summary 27
CHAPTER 2: TOOLING UP 29
Using a Testing Framework 29
Identifying Incorrect Code 33
Designing for Testability 35
Writing the Minimum Required Code 36
Safe Maintenance and Refactoring 36
Runnable Specification 37
Current Open-Source and Commercial Frameworks 37
QUnit 37
D.O.H. 38
Introducing Jasmine 39
Suites and Specs 39
Expectations and Matchers 41
Spies 41
Using a Dependency-Injection Framework 45
What Is Dependency Injection? 45
Making Your Code More Reliable with Dependency Injection 46
Mastering Dependency Injection 47
Case Study: Writing a Lightweight Dependency-Injection Framework 47
Using a Dependency-Injection Framework 55
Current Dependency-Injection Frameworks 56
RequireJS 56
AngularJS 57
Using an Aspect Toolkit 58
Case Study: Caching with and without AOP 58
Implementing Caching without AOP 58
Making Your Code More Reliable with AOP 60
Case Study: Building the Aop.js Module 60
Other AOP Libraries 72
AspectJS 72
AopJS jQuery Plugin 73
YUI's Do Class 73
Conclusion 73
Using a Code-Checking Tool 73
Making Your Code More Reliable with Linting Tools 74
Introducing JSHint 76
Using JSHint 76
If You Don't Run It, Bugs Will Come 79
Alternatives to JSHint 79
JSLint 79
ESLint 79
Strict Mode 80
Summary 80
CHAPTER 3: CONSTRUCTING RELIABLE OBJECTS 81
Using Primitives 81
Using Object Literals 83
Using the Module Pattern 84
Creating Modules ]at ]Will 84
Creating Immediate ]Execution Modules 85
Creating Reliable Modules 86
Using Object Prototypes and Prototypal Inheritance 87
The Default Object Prototype 87
Prototypal Inheritance 88
Prototype Chains 88
Creating Objects with New 89
The new Object Creation Pattern 89
Potential for Bad Things to Happen 90
Enforcing the Use of new 90
Using Classical Inheritance 95
Emulating Classical Inheritance 95
Repetition Killed the Kangaroo 96
Using Functional Inheritance 98
Monkey ]Patching 100
Summary 102
PART II: TESTING PATTERN-BASED CODE
CHAPTER 4: REVIEWING THE BENEFITS OF PATTERNS 107
Case Study 107
Producing More Elegant Code by Using a Broader Vocabulary 108
Producing Reliable Code with Well-Engineered, Well-Tested Building Blocks 109
Summary 110
CHAPTER 5: ENSURING CORRECT USE OF THE CALLBACK PATTERN 111
Understanding the Pattern Through Unit Tests 112
Writing and Testing Code That Uses Callback Functions 112
Writing and Testing Callback Functions 117
Avoiding Problems 121
Flattening the Callback Arrow 121
Minding this 123
Summary 128
CHAPTER 6: ENSURING CORRECT USE OF THE PROMISE PATTERN 129
Understanding Promises Through Unit Tests 130
Using a Promise 130
Constructing and Returning a Promise 135
Testing an XMLHttpRequest 138
Chaining Promises 141
Using a Promise Wrapper 142
Understanding States and Fates 143
Distinguishing Standard Promises from jQuery Promises 143
Summary 143
CHAPTER 7: ENSURING CORRECT USE OF PARTIAL FUNCTION APPLICATION 145
Unit-Testing a Partial Function Application 145
Creating an Aspect for Partial Function Application 147
Distinguishing Between Partial Function Application and Currying 149
Currying 149
Partial Function Application 149
Summary 150
CHAPTER 8: ENSURING CORRECT USE OF THE MEMOIZATION PATTERN 151
Understanding the Pattern Through Unit Tests 152
Adding Memoization with AOP 155
Creating the Memoization Aspect 155
Applying the returnValueCache Aspect to restaurantApi 159
Summary 160
CHAPTER 9: ENSURING CORRECT IMPLEMENTATION OF THE SINGLETON PATTERN 161
Understanding the Pattern Through Unit Tests 162
Implementing a Singleton Shared Cache with an Object Literal 162
Implementing a Singleton Shared Cache with a Module 166
Summary 170
CHAPTER 10: ENSURING CORRECT IMPLEMENTATION OF THE FACTORY PATTERN 173
Writing Unit Tests for a Factory 173
Implementing the Factory Pattern 179
Considering Other Factory Types 181
Summary 181
CHAPTER 11: ENSURING CORRECT IMPLEMENTATION AND USE OF THE SANDBOX PATTERN 183
Understanding the Pattern Through Unit Tests 184
Creating a Widget Sandbox 185
Instantiating a Widget Sandbox 185
Providing Tools to the Widget via the Sandbox 187
Creating and Testing Sandbox Tools 197
Creating Functions for Use with a Sandbox 201
Summary 203
CHAPTER 12: ENSURING CORRECT IMPLEMENTATION OF THE DECORATOR PATTERN 205
Developing a Decorator the Test ]Driven Way 207
Writing a Fake for th