INDEX

A architectural divisions, 449–450 Call Stack Technique, 460 addID , 374–380 Family-Secret Technique, 450–452 addObserver function, 391–395 Imprinting Technique, 452–454 afterEach function, 39–40 Magic Wand Technique, 459–460 anonymous functions as callbacks, 121–123 Mission Impossible Technique, 454–459 AOP (aspect-oriented programming) testing, 473 advices, 58 args parameter, 291 argument checking, 301–303 arguments, 12 aspects, 58 checking, 290–301 caching, 58–59 AOP, 301–303 case studies versus object literals, 84 caching, 58–60 arrays, 291 module building, 60–72 asJSON( ) method, 354 contract enforcement, returned objects, AspectJS, 72–73 283–288 assert method, 282–283 cross-cutting concerns, 58 asynchronous callbacks, 111 libraries attachReturnValidator method, 285–287 AopJS jQuery plugin, 73 AspectJS, 72–73 YUI Do class, 73 B pattern and BDD (behavior-driven development), Jasmine, 39 aspect application, 159–160 beforeEach function, 39–40 aspect creation, 155–158 bind function, 311, 329–334 namespaces, 71–72 binding, this partial function applications, 147–148 COPYRIGHTEDdefault MATERIAL, 312–314 reliability, 60 explicit, 317 versus static solutions, 307–308 implicit, 314–316 testing and, 468–469 new and, 316–317 Aop object, 65–66 boolean data type, 82 AopJS jQuery plugin, 73 Boolean operations, 486–487 apply function, 311 borrow( ) method, 342 testing, 318–320, 472 borrowing methods. See method borrowing

489

bindex.indd 489 13/06/2015 6:04 PM caching – D.O.H. testing framework

C coding standards, 444–449 installation, 436–439 caching, AOP and, 58–60 running, 439–444 call function, 311 testing, 472 testing, 318–320, 472 constructor functions, 90 Callback Pattern, 111–112 properties, 93 callback arrow, 121–123 correctness of code, 22–27 testing functions, 117–121 createATestObject method, 164 unit tests , 149–150 testing code, 112–117 writing code, 112–117 D writing functions, 117–121 callbackFcn, 321–322 D3 (Data-Driven Documents) callbacks, 111–112 case study, 5–15 arrows, 121–123 SVG line code generation, 7–8 asynchronous, 111 data types synchronous, 111 boolean, 82 testing, 117–121, 469 null, 82 testing code, 112–117 number, 82 this variable, 123–128 string, 82 writing, 117–121 undefined, 82 writing code, 112–117 Decorator Pattern, 205–206 chainable methods, 257–258 do-nothing decorators, 209–210 testing, 471–472 error pass through, 208–209 then, 258 fakes, 207–208 this, 259 features, 215–221 unit testing, 259–265 generalization, 222 chaining, Promise, 141–142 pass-through functionality, 210–213 chess analogy for patterns, 107–108 pass-through successes, 213–215 classical inheritance, 95–98 testing, 471 closures, 13 Dependency Inversion Principle (SOLID), 21 code describe function, 39–40 correctness, 22–27 design, 35-37 incorrect, 33–35 development minimum required, 36 small-scale versus large-scale, 5 optimization, 425–433 TDD (test-driven development), 23–24 reuse, 353 DI (), 21, 45–46 code-checking tools, 73–74 case study, 47–55 JSHint, 76–79 frameworks alternatives, 79–80 AngularJS, 57–58 linting tools, 74–76 RequireJS, 56–57 Strict Mode, 80 using, 55–56 coding by contract, 17–18 reliability of code, 46–47 conformance, 435 system testing, 46 ESLint D.O.H. testing framework, 38–39

490

bindex.indd 490 13/06/2015 6:04 PM b DOM – functions

DOM for loops, 109 access, testing, 473 extend function, 361–362 UI tests forEach loops, 109, 126 brittle, 424–425 frameworks code, 418–422 DI (dependency injection), 45–46 event handlers, 422–424 AngularJS, 57–58 done( ) function, 135 code reliability, 46–47 DRY principle, 4, 21–22 RequireJS, 56–57 classical inheritance emulation, 97–98 system testing, 46 functional inheritance, 98–99 using, 55–56 modules, 86 testing monkey patching, 102 D.O.H., 38–39 new object creation, 94 QUnit, 37–38 object literals and, 84 fulfills method, 278–282, 293 primitives and, 82–83 multipleFulfills method, 291 duck typing, 13 functional inheritance, 98–99 functions, 291. See also partial function E applications addObserver, 391–395 ECMAScript, polyfi lls and, 320–329 afterEach, 39–40 emulation, classical inheritance, 95–96 anonymous, 482–483 ESLint, 79–80 callbacks, 121–123 coding standards enforcement, 444–445 arguments, 484–485 custom rule, 445–448 versus object literals, 84 custom rules, 445–449 beforeEach, 39–40 installation bind, 329–334 Node and, 436–439 callbacks (See callbacks) npm and, 439 anonymous functions, 121–123 running, 439–442 testing, 117–121 all JavaScript fi les, 443–444 writing, 117–121 single fi le, 442–443 constructor functions, 90 evaluator function as interface, 276 properties, 93 event handlers, 329–334 declarations, 481–482 expect function, 41 describe, 39–40 extend function, 356–366 done( ), 135 expect, 41 extend, 356–366 F getID( ), 356 factories, 173 getProfile, 253–254 types, 180–181 incrementValue, 312–314 Factory Pattern interpolate, 10 implementing, 179–180 invoking, 485–486 testing, 470 arguments, 484–485 unit testing, 173–179 iterate, 113–115 fates, Promise, 143 line, 8–9

491

M bindex.indd 491 13/06/2015 6:04 PM getID – LRU

line.x, 11–12 Interface Segregation Principle, 273–275 line.y, 11–12 as static data, 276 methods, attaching, 9 interpolate function, 10 nested, 9, 483–484 it function, Jasmine and, 39–40 scope and, 16–17 iterate function, 113–115, 319 as objects, 9, 481 prefetch, 245–246 properties, constructor functions, 93 J register, 51 Jasmine testing framework, 31 return types, 482 afterEach function, 39–40 returning functions, 9 BDD (behavior-driven development), 39 scope, 478–481 beforeEach function, 39–40 segment, 10 describe function, 39–40 setID( ), 356 expect function, 41 toThrow( ), 50 it function, 39–40 validateWithContractNameString, 296 matchers, 41 valIncrementor, 315–316 running tests, 32 specifi cations, 39–40 G spies, 41–44 suites, 39–40 getID( ) function, 356 test doubles, 41–44 getProfile function, 253–254 UI tests, 414–416 JavaScript, features, 4–5 jQuery, plugins, AopJS, 73 H JSHint, 76–80 HTTP GET, 206 JSLint, 79 HTTP POST, 206 HTTP requests, Promise and, 135–138 L I libraries, contracts, 303 line function, 8–9 incrementValue function, 312–314 x-accessor and, 12 inheritance line.x function, 11–12 classical line.y function, 11–12 emulating, 95–96 linting tools, 74–76 repetition, 96–98 Liskov, Barbara, 19–20 functional, 98–99 Liskov Subsitution Principle (SOLID), 19–20 object prototype, 88 literals instance of operator, 90–91 object literals, 83–84 instances, 161 and, 162–166 Interface Segregation Principle (SOLID), 20 loops interfaces for, 109 benefi ts, 272–273 forEach, 109, 126 evaluator function as, 276 LRU (least recently used) cache, 166

492

bindex.indd 492 13/06/2015 6:04 PM b – objects

M functional, 373–380 TDD, 367–373 Mediator Pattern, 395–396 testing, 472 colleagues, 396 traditional , 354 developing, 398–399 creating, 356–373 interfaces, 404–406 testing, 399–401 DRY and, 86 contracts, 403–404 module creation fakeMediator, 401 immediate execution, 85–86 game, 396–397 reliable, 86–87 interface segregation, 402–403 at will, 84–85 mediators, 396 SOLID and, 86 developing, 406–408 modules testing, 408–410 AOP, 60–72 testing, 473 versus scripts, 16 Memoization Pattern, 152 Singleton Pattern and, 166–170 AOP and, 155–160 monkey-patching, 100–102. See also method testing, 470 borrowing unit tests, 152–155 multipleFulfills method, 291 method borrowing, 102, 335–336 borrow( ) method, 342 functions qualifying borrower, 336–338 N objects namespaces, 71–72 aspect, 338–342 nested functions, 9 suitability, 336–343 scope and, 16–17 validator, 342–343 new keyword, 92 side effects, 343–350 this binding and, 316–317 donor objects, 350–351 new object creation pattern, 89–94 testing, 472 null data type, 82 methods. See also chainable methods; functions number data type, 82 asJSON( ), 354 assert, 282–283 attaching to functions, 9 O attachReturnValidator, 285–287 borrow( ), 342 objects createATestObject, 164 construction, testing, 469 fulfills, 278–282 creating, 30–31 iterate, 319 as dictionary, 476–477 multipleFulfills, 291 as functions, 9 then, 131–133, 258, 266–267 literals, 83–84 mixins, 353 versus arguments, 84 addID, 374–380 method borrowing and, 336–343, 350–351 creating, 355–356 new object creation pattern, 89–94 functional, 373–380 primitives, 81–82 traditional, 356–373 properties, adding/removing, 476

493

M bindex.indd 493 13/06/2015 6:04 PM – Promise Pattern

prototype Singleton, 161–162 chains, 88–89 , 223–237 default, 87–88 TDD and inheritance, 88 AOP, 468–469 wrappers, 82 apply function, 472 Observer Pattern, 384 architectural divisions, 473 addObserver function, 391–395 call function, 472 components, 384 callbacks, 469 recentRegistrationsService module, chainable methods, 471–472 385–391 conformance, 472 testing, 473 Decorator, 471 opeators, instance of, 90–91 DOM access, 473 Open/Closed Principle (SOLID), 19 Factory, 470 optimization, profi lers and Mediator, 473 ineffi ciency detection, 425–433 Memoization, 470 premature, 433 method borrowing, 472 mixins, 472 object construction, 469 P Observer, 473 parameters partial function applications, 470 args, 291 promise-based code, 469–470 type free, 290 Proxy, 471 partial function applications, 145 Sandbox, 470–471 AOP (aspect-oriented programming), 147–148 Singleton, 470 versus currying, 149–150 Strategy, 471 testing, 470 vocabulary and, 108–109 unit testing, 145–147 plugins, AopJS, 73 patterns polyfi lls, 320–329 Callback, 111–123 prefetch function, 245–246 case study, 107–108 primitives, 81–82 chess analogy, 107–108 DRY and, 82–83 Decorator, 205–222 SOLID and, 82–83 Factory, 173–181 Promise Pattern Mediator, 395–410 chaining, 141–142 Memoization, 152–160 creating, 135–138 module done( ) function, 135 creating modules, 84–86 fates, 143 immediate execution, 85–86 HTTP requests, 135–138 reliable modules, 86–87 versus jQuery promises, 143 object creation, 89–94 returning, 135–138 Observer, 384–395 states, 143 Promise, 135–143 then method, 131–133 , 240–256 unit testing, 130–135 Sandbox, 183–203 wrappers, 142–143

494

bindex.indd 494 13/06/2015 6:04 PM b promise-based code – TDD

promise-based code, testing, 469–470 testing, 470 properties unit testing, 162 adding/removing, 476 small-scale versus large-scale development, 5 functions, constructor functions, 93 software engineering prototypes DRY principle, 21–22 chains, 88–89 SOLID, 18–21 shared, 88 SOLID, 4 proxies, 239–240 classical inheritance emulation, 97–98 Proxy Pattern, 240–256 Dependency Inversion, 21 testing, 471 functional inheritance, 98–99 Interface Segregation, 20 Liskov Subsitution, 19–20 Q-R modules, 86 QUnit testing framework, 37–38 monkey patching, 102 new object creation, 94 refactoring, 36–37 object literals and, 84 register function, 51 Open/Closed, 19 returnValueCache, 156–160 primitives and, 82–83 Single Responsibility, 18–19 specifi cation. See also testing runnable, 37 S squash analogy, 4–5 Sandbox Pattern, 183–184 states, Promise, 143 testing, 470–471 Strategy Pattern unit testing, 184–185 testing, 471 function creation, 201–203 transportScheduler implementation, tools, 197–201 224–237 widget sandbox, 185–197 Strict Mode, 80 WidgetSandbox constructor string data type, 82 instantiating, 185–186 strings, 291 tools, 187–197 SVG line code generation, 7–8 scope synchronous callbacks, 111 nested functions and, 16–17 system testing, 46 variables, 478–481 scripts versus modules, 16 T segment function, 10 setID( ) function, 356 TDD (test-driven development), 23–24, 48 side effects of method borrowing, 343–351 ad hoc code, 466 Single Responsibility Principle (SOLID), 18–19 benefi ts, 465–466 single-threading, 15–16 development cycle, 466–468 Singleton Pattern, 161–162 extend function, 356–366 shared cache implementation incorrect code identifi cation, 33–35 modules and, 166–170 mixins, 367–373 object literals and, 162–166 patterns

495

M bindex.indd 495 13/06/2015 6:04 PM testing – unit testing

AOP, 468–469 Memoization Pattern, 152–155 apply function, 472 partial function applications, 145–147 architectural divisions, 473 Promise, 130–135 call function, 472 Proxy Pattern, 240–256 callbacks, 469 Sandbox Pattern, 184–203 chainable methods, 471–472 Singleton Pattern, 162–170 conformance, 472 Strategy pattern, 223–237 Decorator, 471 XMLHTTPRequest, 138–141 DOM access, 473 then method, 131–133, 258, 266–267 Factory, 470 this, 311 Mediator, 473 bind and, 329–334 Memoization, 470 binding, 312–317 method borrowing, 472 callbacks, 123–128 mixins, 472 chainable methods and, 259 object construction, 469 toThrow( ) function, 50 Observer, 473 traditional mixins, 354 partial function applications, 470 creating, 356–373 promise-based code, 469–470 type-free semantics, 290 Proxy, 471 Sandbox, 470–471 Singleton, 470 U Strategy, 471 UI (user interface), unit testing UI components, 414–416 component creation, 416–425 unit-testable code, writing, 466 diffi cult-to-test code, 414–416 testing. See also specifi cation; TDD Jasmine test framework, 414–416 apply and, 318–320 undefined data type, 82 bind, 329–334 unit testing, 22–23 call and, 318–320 Callback Pattern callback functions, 117–121 112–117 designing for, 35–36 chainable methods, 259–265 doubles, 41–44 Decorator Pattern ease of, 24–27 do-nothing decorators, 209–210 framework, 29–30 error pass through, 208–209 D.O.H., 38–39 fakes, 207–208 Jasmine, 31–32, 39–44 features, 215–221 QUnit, 37–38 generalization, 222 incorrect code identifi cation, 33–35 pass-through functionality, 210–213 maintenance and, 36–37 pass-through successes, 213–215 runnable, 37 Factory Pattern, 173–179 system testing, 46 framework, Jasmine, 31–32, 39–44 unit testing, 22–23 Memoization Pattern, 152–155 chainable methods, 259–265 partial function applications, 145–147 Decorator Pattern, 205–222 Promise, 130–135 Factory Pattern, 173–179 Proxy Pattern, 240–256

496

bindex.indd 496 13/06/2015 6:04 PM b validateWithContractNameStringld h f unction – YUI Do clas l s

Sandbox Pattern, 184–185 function scope, 478–481 function creation, 201–203 this, 123–128 tools, 197–201 vocabulary, patterns and, 108–109 widget sandbox, 185–197 Singleton Pattern, 162 Strategy Pattern, transportScheduler, W 224–237 WETness (Write Everything Twice or We Enjoy UI Typing), 82 component creation, 414–416 wrappers, 82 diffi cult-to-test code, 414–416 Promise, 142–143

V X-Y-Z validateWithContractNameString function, x-accessor, line and, 12 296 XMLHttpRequest, 136–138 valIncrementor function, 315–316 testing, 138–141 variables declarations, 477–478 YUI Do class, 73

497

M bindex.indd 497 13/06/2015 6:04 PM