Secrets of the JavaScript Ninja, Second Edition
John Resig, Bear Bibeault, and Josip Maras
  • August 2016
  • ISBN 9781617292859
  • 464 pages
  • printed in black & white

Essential reading for developers of any discipline ... with powerful techniques to improve your JavaScript.

Becky Huett, Big Shovel Labs


An eBook copy of the previous edition, Secrets of the JavaScript Ninja (First Edition), is included at no additional cost. It will be automatically added to your Manning account within 24 hours of purchase.

More than ever, the web is a universal platform for all types of applications, and JavaScript is the language of the web. If you're serious about web development, it's not enough to be a decent JavaScript coder. You need to be ninja-stealthy, efficient, and ready for anything. This book shows you how.

Table of Contents detailed table of contents

Part 1: Warming up

1. JavaScript is Everywhere

1.1. Understanding the JavaScript language

1.1.1. How will JavaScript evolve?

1.1.2. Transpilers give us access to tomorrow’s JavaScript today

1.2. Understanding the browser

1.3. Using current best practices

1.3.1. Debugging

1.3.2. Testing

1.3.3. Performance analysis

1.4. Boosting skill transferability

1.5. Summary

2. Building the page at runtime

2.1. The lifecycle overview

2.2. The page building phase

2.2.1. Parsing the HTML and building the DOM

2.2.2. Executing JavaScript code

2.3. Event handling

2.3.1. Event handling overview

2.3.2. Registering event handlers

2.3.3. Handling events

2.4. Summary

2.5. Exercises

Part 2: Understanding functions

3. First-class functions for the Novice: Definitions and arguments

3.1. What's with the functional difference?

3.1.1. Functions as first-class objects

3.1.2. Callback functions

3.2. Fun with functions as objects

3.2.1. Storing functions

3.2.2. Self-memoizing functions

3.3. Defining functions

3.3.1. Function declarations and function expressions

3.3.2. Arrow functions

3.4. Arguments and function parameters

3.4.1. Rest parameters

3.4.2. Default parameters

3.5. Summary

3.6. Exercises

4. Functions for the Journeyman: Understanding function invocation

4.1. Implicit function parameters

4.1.1. The arguments parameter

4.1.2. The this parameter: introducing the function context

4.2. Invoking functions

4.2.1. Invocation as a function

4.2.2. Invocation as a method

4.2.3. Invoking functions as constructors

4.2.4. Invocation with the apply and call methods

4.3. Fixing the problem of function contexts

4.3.1. Using arrow functions to get around function contexts

4.3.2. Using the bind method

4.4. Summary

4.5. Exercises

5. Functions for the Master: Closures and Scopes

5.1. Understanding closures

5.2. Putting closures to work

5.2.1. Mimicking private variables

5.2.2. Using closures with callbacks

5.3. Tracking code execution with execution contexts

5.4. Keeping track of identifiers with lexical environments

5.4.1. Code nesting

5.4.2. Code nesting and lexical environments

5.5. Understanding different types of JavaScript variables

5.5.1. Variable mutability

5.5.2. Variable definition keywords and lexical environments

5.5.3. Registering identifiers within lexical environments

5.6. Exploring how closures work

5.6.1. Revisiting mimicking private variables with closures

5.6.2. Private variables caveat

5.6.3. Revisiting the closures and callbacks example

5.7. Summary

5.8. Exercises

6. Functions for the future: Generators and Promises

6.1. Generators and promises make our async code elegant

6.2. Generator functions

6.2.1. Controlling the generator through the iterator object

6.2.2. Using generators

6.2.3. Communicating with a generator

6.2.4. Generator under the hood

6.3. Promises

6.3.1. The problems with simple callbacks

6.3.2. Diving into promises

6.3.3. Rejecting promises

6.3.4. Creating our first real-world promise

6.3.5. Chaining promises

6.3.6. Waiting for a number of promises

6.4. Combining generators and promises

6.4.1. Looking onward to ES7 — async functions

6.5. Summary

6.6. Exercises

Part 3: Digging into objects & fortifying your code

7. Object-orientation with prototypes

7.1. Understanding prototypes

7.2. Object construction and prototypes

7.2.1. Instance properties

7.2.2. Dynamicity caveats

7.2.3. Object typing via constructors

7.3. Inheritance

7.3.1. The problem of overriding the constructor property

7.3.2. The instanceof operator

7.4. JavaScript "classes" in ES6

7.4.1. Using the class keyword

7.4.2. Implementing inheritance

7.5. Summary

7.6. Exercises

8. Guarding access to objects

8.1. Controlling access to properties with getters and setters

8.1.1. Defining getters and setters

8.1.2. Using getters and setters to validate property values

8.1.3. Using getters and setters to define computed properties

8.2. Proxies

8.2.1. Using proxies for logging

8.2.2. Using proxies for measuring performance

8.2.3. Using proxies to auto-populate properties

8.2.4. Using proxies to implement negative array indexes

8.2.5. Performance costs of proxies

8.3. Summary

8.4. Exercises

9. Dealing with collections

9.1. Arrays

9.1.1. Creating arrays

9.1.2. Adding and removing items at either end of an array

9.1.3. Adding and removing items at any array location

9.1.4. Common operations on array

9.1.5. Reusing built-in array functions

9.2. Maps

9.2.1. Don't use objects as maps

9.2.2. Creating our first Map

9.2.3. Iterating over maps

9.3. Sets

9.3.1. Creating our first Set

9.3.2. Union of sets

9.3.3. Intersection of sets

9.3.4. Difference of sets

9.4. Summary

9.5. Exercises

10. Wrangling regular expressions

10.1. Why regular expressions rock

10.2. A regular expression refresher

10.2.1. Regular expressions explained

10.2.2. Terms and operators

10.3. Compiling regular expressions

10.4. Capturing matching segments

10.4.1. Performing simple captures

10.4.2. Matching using global expressions

10.4.3. Referencing captures

10.4.4. Non-capturing groups

10.5. Replacing using functions

10.6. Solving common problems with regular expressions

10.6.1. Matching newlines

10.6.2. Unicode

10.6.3. Escaped characters

10.7. Summary

10.8. Exercises

11. Code modularization techniques

11.1. Modularizing code in pre-ES6 JavaScript

11.1.1. Using objects, closures, and immediate functions to specify modules

11.1.2. Modularizing JavaScript applications with AMD and CommonJS

11.2. ES6 modules

11.2.1. Exporting and importing functionality

11.3. Summary

11.4. Exercises

Part 4: Browser Reconnaissance

12. Working the DOM

12.1. Injecting HTML into the DOM

12.1.1. Converting HTML to DOM

12.1.2. Inserting into the document

12.2. DOM attributes and properties

12.3. Styling attribute headaches

12.3.1. Where are my styles?

12.3.2. Style property naming

12.3.3. Fetching computed styles

12.3.4. Conversion of pixel values

12.3.5. Measuring heights and widths

12.4. Layout trashing

12.5. Summary

12.6. Exercises

13. Surviving events

13.1. Diving into the event loop

13.1.1. An example with only macrotasks

13.1.2. An example with both macro- and microtasks

13.2. Taming timers: Timeouts and Intervals

13.2.1. Timer execution within the event loop

13.2.2. Dealing with computationally expensive processing

13.3. Working with events

13.3.1. Propagating events through the DOM

13.3.2. Custom events

13.4. Summary

13.5. Exercises

14. Developing cross-browser strategies

14.1. Cross-browser considerations

14.2. The five major development concerns

14.2.1. Browser bugs and differences

14.2.2. Browser bug fixes

14.2.3. Living with external code and markup

14.2.4. Regressions

14.3. Implementation strategies

14.3.1. Safe cross-browser fixes

14.3.2. Feature detection and polyfills

14.3.3. Untestable browser issues

14.4. Reducing assumptions

14.5. Summary

14.6. Exercises


Appendix A: Additional ES6 features

A.1. Template literals

A.2. Destructuring

A.3. Enhanced object literals

Appendix B: Arming with testing and debugging

B.1. Web Developer tools

B.2. Debugging code

B.2.1. Logging

B.2.2. Breakpoints

B.3. Creating tests

B.4. The fundamentals of a testing framework

B.4.1. The assertion

Appendix C: Exercise answers

C.1. Chapter 1. Enter the Ninja

C.2. Chapter 2. Building the page at runtime

C.3. Chapter 3. First-class functions for the Novice: Definitions and arguments

C.4. Chapter 4. Functions for the Journeyman: Understanding function invocation

C.5. Chapter 5. Functions for the Master: Closures and scopes

C.6. Chapter 6. Functions for the future: Generators and Promises

C.7. Chapter 7. Object orientation with prototypes

C.8. Chapter 8. Guarding access to objects

C.9. Chapter 9. Dealing with collections

C.10. Chapter 10. Wrangling regular expressions

C.11. Chapter 11. Code modularization techniques

C.12. Chapter 12. Working the DOM

C.13. Chapter 13. Surviving events

C.14. Chapter 14. Developing cross-browser strategies

About the Technology

JavaScript is rapidly becoming a universal language for every type of application, whether on the web, on the desktop, in the cloud, or on mobile devices. When you become a JavaScript pro, you have a powerful skill set that's usable across all these domains.

About the book

Secrets of the JavaScript Ninja, Second Edition uses practical examples to clearly illustrate each core concept and technique. This completely revised edition shows you how to master key JavaScript concepts such as functions, closures, objects, prototypes, and promises. It covers APIs such as the DOM, events, and timers. You'll discover best practice techniques such as testing, and cross-browser development, all taught from the perspective of skilled JavaScript practitioners.

What's inside

  • Writing more effective code with functions, objects, and closures
  • Learning to avoid JavaScript application pitfalls
  • Using regular expressions to write succinct text-processing code
  • Managing asynchronous code with promises
  • Fully revised to cover concepts from ES6 and ES7

About the reader

You don't have to be a ninja to read this book—just be willing to become one. Are you ready?

About the authors

John Resig is an acknowledged JavaScript authority and the creator of the jQuery library. Bear Bibeault is a web developer and author of the first edition, as well as coauthor of Ajax in Practice, Prototype and Scriptaculous in Action, and jQuery in Action from Manning. Josip Maras is a post-doctoral researcher and teacher.

combo $44.99 pBook + eBook + liveBook
eBook $35.99 pdf + ePub + kindle + liveBook
Already own this book?
You can add audio for liveBook from your bookshelf!

FREE domestic shipping on three or more pBooks

Excellent and comprehensive insight into the magic of functions and closures for the efficient use of JavaScript.

Gerd Klevesaat, Siemens

The essential resource for moving your JavaScript skills to the next level.

David Starkey, Blum

Helps you master both the stealthy and bold techniques of modern JavaScript.

Christopher Haupt, New Relic Inc.