Details

Reliable JavaScript


Reliable JavaScript

How to Code Safely in the World's Most Dangerous Language
1. Aufl.

von: Lawrence D. Spencer, Seth H. Richards

32,99 €

Verlag: Wiley
Format: EPUB
Veröffentl.: 29.06.2015
ISBN/EAN: 9781119028741
Sprache: englisch
Anzahl Seiten: 528

DRM-geschütztes eBook, Sie benötigen z.B. Adobe Digital Editions und eine Adobe ID zum Lesen.

Beschreibungen

<b>Create more robust applications with a test-first approach to JavaScript</b> <p><i>Reliable JavaScript, How to Code Safely in the World's Most Dangerous Language</i> 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. <p>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. <ul> <li>Write more concise and elegant code by thinking in JavaScript</li> <li>Test the implementation and use of common design patterns</li> <li>Master the use of advanced JavaScript features</li> <li>Ensure your code's conformance to your organization's standards</li> </ul> <p>If you're ready to step up your code and develop more complete software solutions, <i>Reliable JavaScript</i> is your essential resource.
INTRODUCTION xxi <p><b>PART I: LAYING A SOLID FOUNDATION</b></p> <p><b>CHAPTER 1: PRACTICING SKILLFUL SOFTWARE 3</b></p> <p>Writing Code That Starts Correct 4</p> <p>Mastering the Features of JavaScript 4</p> <p>Case Study: D3.js 5</p> <p>JavaScript Is Single-Threaded 15</p> <p>Avoiding JavaScript’s Pitfalls in Larger Systems 16</p> <p>Scripts Are Not Modules 16</p> <p>Nested Functions Control Scope 16</p> <p>Coding by Contract 17</p> <p>Applying the Principles of Software Engineering 18</p> <p>The SOLID Principles 18</p> <p>The DRY Principle 21</p> <p>Writing Code That Stays Correct 22</p> <p>Investing for the Future with Unit Tests 22</p> <p>Practicing Test-Driven Development 23</p> <p>Engineering Your Code to Be Easy to Test 24</p> <p>Summary 27</p> <p><b>CHAPTER 2: TOOLING UP 29</b></p> <p>Using a Testing Framework 29</p> <p>Identifying Incorrect Code 33</p> <p>Designing for Testability 35</p> <p>Writing the Minimum Required Code 36</p> <p>Safe Maintenance and Refactoring 36</p> <p>Runnable Specification 37</p> <p>Current Open-Source and Commercial Frameworks 37</p> <p>QUnit 37</p> <p>D.O.H. 38</p> <p>Introducing Jasmine 39</p> <p>Suites and Specs 39</p> <p>Expectations and Matchers 41</p> <p>Spies 41</p> <p>Using a Dependency-Injection Framework 45</p> <p>What Is Dependency Injection? 45</p> <p>Making Your Code More Reliable with Dependency Injection 46</p> <p>Mastering Dependency Injection 47</p> <p>Case Study: Writing a Lightweight Dependency-Injection Framework 47</p> <p>Using a Dependency-Injection Framework 55</p> <p>Current Dependency-Injection Frameworks 56</p> <p>RequireJS 56</p> <p>AngularJS 57</p> <p>Using an Aspect Toolkit 58</p> <p>Case Study: Caching with and without AOP 58</p> <p>Implementing Caching without AOP 58</p> <p>Making Your Code More Reliable with AOP 60</p> <p>Case Study: Building the Aop.js Module 60</p> <p>Other AOP Libraries 72</p> <p>AspectJS 72</p> <p>AopJS jQuery Plugin 73</p> <p>YUI’s Do Class 73</p> <p>Conclusion 73</p> <p>Using a Code-Checking Tool 73</p> <p>Making Your Code More Reliable with Linting Tools 74</p> <p>Introducing JSHint 76</p> <p>Using JSHint 76</p> <p>If You Don’t Run It, Bugs Will Come 79</p> <p>Alternatives to JSHint 79</p> <p>JSLint 79</p> <p>ESLint 79</p> <p>Strict Mode 80</p> <p>Summary 80</p> <p><b>CHAPTER 3: CONSTRUCTING RELIABLE OBJECTS 81</b></p> <p>Using Primitives 81</p> <p>Using Object Literals 83</p> <p>Using the Module Pattern 84</p> <p>Creating Modules?]at?]Will 84</p> <p>Creating Immediate?]Execution Modules 85</p> <p>Creating Reliable Modules 86</p> <p>Using Object Prototypes and Prototypal Inheritance 87</p> <p>The Default Object Prototype 87</p> <p>Prototypal Inheritance 88</p> <p>Prototype Chains 88</p> <p>Creating Objects with New 89</p> <p>The new Object Creation Pattern 89</p> <p>Potential for Bad Things to Happen 90</p> <p>Enforcing the Use of new 90</p> <p>Using Classical Inheritance 95</p> <p>Emulating Classical Inheritance 95</p> <p>Repetition Killed the Kangaroo 96</p> <p>Using Functional Inheritance 98</p> <p>Monkey?]Patching 100</p> <p>Summary 102</p> <p><b>PART II: TESTING PATTERN-BASED CODE</b></p> <p><b>CHAPTER 4: REVIEWING THE BENEFITS OF PATTERNS 107</b></p> <p>Case Study 107</p> <p>Producing More Elegant Code by Using a Broader Vocabulary 108</p> <p>Producing Reliable Code with Well-Engineered, Well-Tested Building Blocks 109</p> <p>Summary 110</p> <p><b>CHAPTER 5: ENSURING CORRECT USE OF THE CALLBACK PATTERN 111</b></p> <p>Understanding the Pattern Through Unit Tests 112</p> <p>Writing and Testing Code That Uses Callback Functions 112</p> <p>Writing and Testing Callback Functions 117</p> <p>Avoiding Problems 121</p> <p>Flattening the Callback Arrow 121</p> <p>Minding this 123</p> <p>Summary 128</p> <p><b>CHAPTER 6: ENSURING CORRECT USE OF THE PROMISE PATTERN 129</b></p> <p>Understanding Promises Through Unit Tests 130</p> <p>Using a Promise 130</p> <p>Constructing and Returning a Promise 135</p> <p>Testing an XMLHttpRequest 138</p> <p>Chaining Promises 141</p> <p>Using a Promise Wrapper 142</p> <p>Understanding States and Fates 143</p> <p>Distinguishing Standard Promises from jQuery Promises 143</p> <p>Summary 143</p> <p><b>CHAPTER 7: ENSURING CORRECT USE OF PARTIAL FUNCTION APPLICATION 145</b></p> <p>Unit-Testing a Partial Function Application 145</p> <p>Creating an Aspect for Partial Function Application 147</p> <p>Distinguishing Between Partial Function Application and Currying 149</p> <p>Currying 149</p> <p>Partial Function Application 149</p> <p>Summary 150</p> <p><b>CHAPTER 8: ENSURING CORRECT USE OF THE MEMOIZATION PATTERN 151</b></p> <p>Understanding the Pattern Through Unit Tests 152</p> <p>Adding Memoization with AOP 155</p> <p>Creating the Memoization Aspect 155</p> <p>Applying the returnValueCache Aspect to restaurantApi 159</p> <p>Summary 160</p> <p><b>CHAPTER 9: ENSURING CORRECT IMPLEMENTATION OF THE SINGLETON PATTERN 161</b></p> <p>Understanding the Pattern Through Unit Tests 162</p> <p>Implementing a Singleton Shared Cache with an Object Literal 162</p> <p>Implementing a Singleton Shared Cache with a Module 166</p> <p>Summary 170</p> <p><b>CHAPTER 10: ENSURING CORRECT IMPLEMENTATION OF THE FACTORY PATTERN 173</b></p> <p>Writing Unit Tests for a Factory 173</p> <p>Implementing the Factory Pattern 179</p> <p>Considering Other Factory Types 181</p> <p>Summary 181</p> <p><b>CHAPTER 11: ENSURING CORRECT IMPLEMENTATION AND USE OF THE SANDBOX PATTERN 183</b></p> <p>Understanding the Pattern Through Unit Tests 184</p> <p>Creating a Widget Sandbox 185</p> <p>Instantiating a Widget Sandbox 185</p> <p>Providing Tools to the Widget via the Sandbox 187</p> <p>Creating and Testing Sandbox Tools 197</p> <p>Creating Functions for Use with a Sandbox 201</p> <p>Summary 203</p> <p><b>CHAPTER 12: ENSURING CORRECT IMPLEMENTATION OF THE DECORATOR PATTERN 205</b></p> <p>Developing a Decorator the Test?]Driven Way 207</p> <p>Writing a Fake for the Decorated Object 207</p> <p>Writing Tests for Pass?]Through of Errors 208</p> <p>Writing a Do?]Nothing Decorator 209</p> <p>Adding Pass?]Through Functionality to the Decorator 210</p> <p>Verifying Pass?]Through of Successes 213</p> <p>Adding the Decorator’s Features 215</p> <p>Generalizing the Decorator 222</p> <p>Summary 222</p> <p><b>CHAPTER 13: ENSURING CORRECT IMPLEMENTATION OF THE STRATEGY PATTERN 223</b></p> <p>Understanding the Pattern Through Unit Tests 223</p> <p>Implementing the transportScheduler Without the Strategy Pattern 224</p> <p>Implementing the transportScheduler Using the Strategy Pattern 226</p> <p>Creating transportScheduler Using Test?]Driven Development 227</p> <p>Creating a Strategy for Use with transportScheduler 235</p> <p>Summary 237</p> <p><b>CHAPTER 14: ENSURING CORRECT IMPLEMENTATION OF THE PROXY PATTERN 239</b></p> <p>Developing a Proxy the Test?]Driven Way 240</p> <p>Summary 256</p> <p><b>CHAPTER 15: ENSURING CORRECT IMPLEMENTATION OF CHAINABLE METHODS 257</b></p> <p>Understanding the Pattern Through Unit Tests 259</p> <p>Chaining then 266</p> <p>Summary 267</p> <p><b>PART III: TESTING AND WRITING WITH ADVANCED JAVASCRIPT FEATURES<br /><br />CHAPTER 16: CONFORMING TO INTERFACES IN AN INTERFACE-FREE LANGUAGE 271</b></p> <p>Understanding the Benefits of Interfaces 272</p> <p>Understanding the Interface Segregation Principle 273</p> <p>Using Test-Driven Development to Create a Contract Registry 275</p> <p>Defining a Contract 275</p> <p>Determining Whether a Contract Is Fulfilled 278</p> <p>Asserting That a Contract Is Fulfi lled 282</p> <p>Bypassing Contract Enforcement 283</p> <p>Creating an Aspect to Enforce a Contract on a Returned (Created) Object 283</p> <p>Summary 288</p> <p><b>CHAPTER 17: ENSURING CORRECT ARGUMENT TYPES 289</b></p> <p>Introduction 289</p> <p>Understanding the Opportunities and Risks Posed by JavaScript’s Type-Free Parameters 290</p> <p>Extending the ContractRegistry to Check Arguments 290</p> <p>Scoping Out the Task 291</p> <p>Determining Whether Every Variable in a Set Fulfi lls Its Contract 291</p> <p>Asserting That Every Variable in a Set Fulfi lls Its Contract 300</p> <p>Packaging Argument-Checking in an Aspect 301</p> <p>Supporting Contract Libraries 303</p> <p>Putting It All Together 303</p> <p>Creating the Contracts Modules 304</p> <p>Creating the Application’s ContractRegistry 307</p> <p>Bypassing Contracts for Production 307</p> <p>Comparing the Aspect-Oriented Solution to a Static Solution 307</p> <p>Considering the Advantages of TypeScript 308</p> <p>Considering the Advantages of Aspects 308</p> <p>Summary 308</p> <p><b>CHAPTER 18: ENSURING CORRECT USE OF CALL, APPLY, AND BIND 311</b></p> <p>Exploring How this Is Bound 312</p> <p>Default Binding 312</p> <p>Default Binding and strict Mode 313</p> <p>Implicit Binding 314</p> <p>new Binding 316</p> <p>Explicit Binding 317</p> <p>Creating and Testing Code That Uses call, apply, and bind 317</p> <p>Using call and apply 318</p> <p>Creating an Array.prototype.forEach Polyfill Using Test-Driven Development 320</p> <p>Using bind 329</p> <p>Summary 334</p> <p><b>CHAPTER 19: ENSURING THE CORRECT USE OF METHODBORROWING 335</b></p> <p>Ensuring the Borrowing Object Is Suitable 336</p> <p>Making the Borrowed Function Qualify the Borrower 336</p> <p>Attaching an Aspect to the Borrowed Object 338</p> <p>Using a borrow() Method 342</p> <p>Adding an Object?]Validator to the ContractRegistry 342</p> <p>Anticipating Side Effects on the Borrower 343</p> <p>Considering Side Effects from an Isolated Function 343</p> <p>Considering Side Effects from a Function That Calls Other Functions 345</p> <p>Anticipating Side Effects on the Donor Object 351</p> <p>Summary 351</p> <p><b>CHAPTER 20: ENSURING CORRECT USE OF MIXINS 353</b></p> <p>Creating and Using Mixins 355</p> <p>Creating and Using a Traditional Mixin 356</p> <p>Creating the extend Function Using Test?]driven Development 356</p> <p>Creating a Traditional Mixin Using Test?]driven Development 367</p> <p>Creating and Using a Functional Mixin 373</p> <p>Summary 380</p> <p><b>CHAPTER 21: TESTING ADVANCED PROGRAM ARCHITECTURES 383</b></p> <p>Ensuring Reliable Use of the Observer Pattern 384</p> <p>Examining the Observer Pattern 384</p> <p>Enhancing the Reliability of the Observer Pattern 391</p> <p>Ensuring Reliable Use of the Mediator Pattern 395</p> <p>Examining the Mediator Pattern 396</p> <p>Enhancing the Reliability of Mediator-Based Code 397</p> <p>Developing a Colleague 398</p> <p>Testing a Colleague 399</p> <p>Segregating the Mediator’s Interfaces 402</p> <p>Deciding Where to Put the Contracts 403</p> <p>Ensuring the Colleague Gets a Mediator with the Expected Interface 404</p> <p>Developing a Mediator 406</p> <p>Testing the Mediator 408</p> <p>Summary 410</p> <p><b>PART IV: SPECIAL SUBJECTS IN TESTING</b></p> <p><b>CHAPTER 22: TESTING ADVANCED PROGRAM ARCHITECTURES 383</b></p> <p>Ensuring Reliable Use of the Observer Pattern 384</p> <p>Examining the Observer Pattern 384</p> <p>Enhancing the Reliability of the Observer Pattern 391</p> <p>Ensuring Reliable Use of the Mediator Pattern 395</p> <p>Examining the Mediator Pattern 396</p> <p>Enhancing the Reliability of Mediator-Based Code 397</p> <p>Developing a Colleague 398</p> <p>Testing a Colleague 399</p> <p>Segregating the Mediator’s Interfaces 402</p> <p>Deciding Where to Put the Contracts 403</p> <p>Ensuring the Colleague Gets a Mediator with the Expected Interface 404</p> <p>Developing a Mediator 406</p> <p>Testing the Mediator 408</p> <p>Summary 410</p> <p><b>CHAPTER 23: ENSURING CONFORMANCE TO STANDARDS 435</b></p> <p>Using ESLint 436</p> <p>Installing ESLint 436</p> <p>Installing Node and npm 436</p> <p>Installing ESLint Using npm 439</p> <p>Running ESLint 439</p> <p>Executing ESLint on a Single File 442</p> <p>Executing ESLint on All the JavaScript Files in a Directory 443</p> <p>Enforcing Coding Standards with ESLint 444</p> <p>Creating a Custom ESLint Rule 445</p> <p>Running ESLint with Custom Rules 448</p> <p>Enforcing Architectural Divisions 449</p> <p>The Family-Secret Technique 450</p> <p>The Imprinting Technique 452</p> <p>The Mission Impossible Technique 454</p> <p>The Magic Wand Technique 459</p> <p>Do Not Use the Call Stack Technique 460</p> <p>Other Techniques 460</p> <p>Other Architectures 460</p> <p>Summary 460</p> <p><b>PART V: SUMMARY</b></p> <p><b>CHAPTER 24: SUMMARY OF THE PRINCIPLES OF TEST-DRIVEN DEVELOPMENT 465</b></p> <p>Recalling Why Test-Driven Development Is Worthwhile 465</p> <p>Practicing Test-Driven Development 466</p> <p>Writing Unit-Testable Code 466</p> <p>Mastering the Mechanics of Test-Driven Development 466</p> <p>Writing the Test Before the Code 467</p> <p>Keeping Your Tests DRY 467</p> <p>Testing Error Conditions First 467</p> <p>Testing the Simple before the Complex 467</p> <p>Being Specifi c 467</p> <p>Testing Just One Thing 468</p> <p>Your Test Data Are Just As Important As the Test 468</p> <p>Using Jasmine Effectively 468</p> <p>Testing the Patterns in This Book 468</p> <p>Testing Aspect-Oriented Programming 468</p> <p>Testing Object Construction 469</p> <p>Testing Callbacks 469</p> <p>Testing Promise-Based Code 469</p> <p>Testing a Partial Function Application 470</p> <p>Testing Memoization 470</p> <p>Testing a Singleton 470</p> <p>Testing a Factory Method 470</p> <p>Testing a Sandbox 470</p> <p>Testing the Decorator Pattern 471</p> <p>Testing the Strategy Pattern 471</p> <p>Testing the Proxy Pattern 471</p> <p>Testing Chainable Methods 471</p> <p>Testing Conformance to an Interface 472</p> <p>Testing the Use of call and apply 472</p> <p>Testing the Method-Borrowing Pattern 472</p> <p>Testing Mixins 472</p> <p>Testing Mediators and Observers 473</p> <p>Testing DOM Access 473</p> <p>Tests to Enforce Architectural Divisions 473</p> <p>Summary 473</p> <p><b>CHAPTER 25: SUMMARY OF JAVASCRIPT IDIOMS IN THIS BOOK 475</b></p> <p>Reviewing Objects 475</p> <p>Object Properties May Be Added and Removed 476</p> <p>Objects May Be Used as a Dictionary 476</p> <p>Reviewing Variables 477</p> <p>Variable Declarations Are Hoisted 477</p> <p>Variables Have Function Scope 478</p> <p>Reviewing Functions 481</p> <p>Functions Are Objects 481</p> <p>Functions Declarations Are Hoisted 481</p> <p>Functions Don’t Have Return Types 482</p> <p>Functions May Be Anonymous 482</p> <p>Functions May Be Nested 483</p> <p>Functions May Be Invoked with Any Number of Arguments 484</p> <p>Functions May Be Invoked Immediately 485</p> <p>Reviewing Boolean Operations 486</p> <p>Types May Be Coerced When Testing Equality 486</p> <p>Values May Be Truthy or Falsy 487</p> <p>Summary 487</p> <p>INDEX 489</p>
<p><b>About the authors</b> <p><b>Larry Spencer</b> leads an international team of developers at ScerIS, a software and services company near Boston. He has over 35 years' experience as an executive, developer, consultant, teacher, and frequent presenter at programming conferences. <p><b>Seth Richards</b> has been crafting software professionally since 2002. His past work focused on web-based, enterprise-class geographic information system applications. <p>Visit us at wrox.com where you have access to free code samples, Programmer to Programmer forums, and discussions on the latest happenings in the industry from around the world.
<p><b>Test first, then code, for more robust large-scale JavaScript applications</b> <p>Experienced developers still struggle with the failure of large-scale JavaScript applications that collapse under their own weight. <i>Reliable JavaScript</i> provides the solution in the form of a test-driven workflow that produces robust applications that stand the test of time. This book uses unit tests to explain, demonstrate, and employ a wide variety of JavaScript patterns and architectures. As you follow along, you will become an expert in the test-driven development of JavaScript that is suitable for commercial software. If you are ready to bring a high level of software engineering to your JavaScript development, this book is for you. <p><i>Reliable JavaScript:</i> <ul> <li>Shows you how to write code that's verified correct the first time, and has the test coverage to remain correct throughout subsequent maintenance</li> <li>Surveys the current landscape of JavaScript frameworks for testing, dependency injection, and aspect-oriented programming</li> <li>Explains testability characteristics and how to construct objects to be testable</li> <li>Demonstrates the implementation and use of common design patterns and advanced JavaScript features and architectures</li> <li>Covers DOM access testing, including excessive updates, excessive access in loops, and XHR/JSONP requests</li> <li>Provides all of the book's code both in the text and on the companion website</li> </ul> <p><b>Wrox Professional guides</b> are planned and written by working programmers to meet the real-world needs of programmers, developers, and IT professionals. Focused and relevant, they address the issues technology professionals face every day. They provide examples, practical solutions, and expert education in new technologies, all designed to help programmers do a better job.

Diese Produkte könnten Sie auch interessieren:

Facebook Marketing
Facebook Marketing
von: Chris Treadaway, Mari Smith
PDF ebook
23,99 €
Ajax For Dummies
Ajax For Dummies
von: Steve Holzner
PDF ebook
22,99 €
The Web's Awake
The Web's Awake
von: Philip D. Tetlow
PDF ebook
69,99 €