You are previewing Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript.

Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript

Cover of Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript by David Herman Published by Addison-Wesley Professional
  1. Title Page
  2. Copyright Page
  3. Praise for Effective JavaScript
  4. Dedication Page
  5. Contents
  6. Foreword
  7. Preface
    1. JavaScript versus ECMAScript
    2. On the Web
    3. A Note on Concurrency
  8. Acknowledgments
  9. About the Author
  10. 1. Accustoming Yourself to JavaScript
    1. Item 1: Know Which JavaScript You Are Using
    2. Item 2: Understand JavaScript’s Floating-Point Numbers
    3. Item 3: Beware of Implicit Coercions
    4. Item 4: Prefer Primitives to Object Wrappers
    5. Item 5: Avoid using == with Mixed Types
    6. Item 6: Learn the Limits of Semicolon Insertion
    7. Item 7: Think of Strings As Sequences of 16-Bit Code Units
  11. 2. Variable Scope
    1. Item 8: Minimize Use of the Global Object
    2. Item 9: Always Declare Local Variables
    3. Item 10: Avoid with
    4. Item 11: Get Comfortable with Closures
    5. Item 12: Understand Variable Hoisting
    6. Item 13: Use Immediately Invoked Function Expressions to Create Local Scopes
    7. Item 14: Beware of Unportable Scoping of Named Function Expressions
    8. Item 15: Beware of Unportable Scoping of Block-Local Function Declarations
    9. Item 16: Avoid Creating Local Variables with eval
    10. Item 17: Prefer Indirect eval to Direct eval
  12. 3. Working with Functions
    1. Item 18: Understand the Difference between Function, Method, and Constructor Calls
    2. Item 19: Get Comfortable Using Higher-Order Functions
    3. Item 20: Use call to Call Methods with a Custom Receiver
    4. Item 21: Use apply to Call Functions with Different Numbers of Arguments
    5. Item 22: Use arguments to Create Variadic Functions
    6. Item 23: Never Modify the arguments Object
    7. Item 24: Use a Variable to Save a Reference to arguments
    8. Item 25: Use bind to Extract Methods with a Fixed Receiver
    9. Item 26: Use bind to Curry Functions
    10. Item 27: Prefer Closures to Strings for Encapsulating Code
    11. Item 28: Avoid Relying on the toString Method of Functions
    12. Item 29: Avoid Nonstandard Stack Inspection Properties
  13. 4. Objects and Prototypes
    1. Item 30: Understand the Difference between prototype, getPrototypeOf, and __proto__
    2. Item 31: Prefer Object.getPrototypeOf to __proto__
    3. Item 32: Never Modify __proto__
    4. Item 33: Make Your Constructors new-Agnostic
    5. Item 34: Store Methods on Prototypes
    6. Item 35: Use Closures to Store Private Data
    7. Item 36: Store Instance State Only on Instance Objects
    8. Item 37: Recognize the Implicit Binding of this
    9. Item 38: Call Superclass Constructors from Subclass Constructors
    10. Item 39: Never Reuse Superclass Property Names
    11. Item 40: Avoid Inheriting from Standard Classes
    12. Item 41: Treat Prototypes As an Implementation Detail
    13. Item 42: Avoid Reckless Monkey-Patching
  14. 5. Arrays and Dictionaries
    1. Item 43: Build Lightweight Dictionaries from Direct Instances of Object
    2. Item 44: Use null Prototypes to Prevent Prototype Pollution
    3. Item 45: Use hasOwnProperty to Protect Against Prototype Pollution
    4. Item 46: Prefer Arrays to Dictionaries for Ordered Collections
    5. Item 47: Never Add Enumerable Properties to Object.prototype
    6. Item 48: Avoid Modifying an Object during Enumeration
    7. Item 49: Prefer for Loops to Loops for Array Iteration
    8. Item 50: Prefer Iteration Methods to Loops
    9. Item 51: Reuse Generic Array Methods on Array-Like Objects
    10. Item 52: Prefer Array Literals to the Array Constructor
  15. 6. Library and API Design
    1. Item 53: Maintain Consistent Conventions
    2. Item 54: Treat undefined As “No Value”
    3. Item 55: Accept Options Objects for Keyword Arguments
    4. Item 56: Avoid Unnecessary State
    5. Item 57: Use Structural Typing for Flexible Interfaces
    6. Item 58: Distinguish between Array and Array-Like
    7. Item 59: Avoid Excessive Coercion
    8. Item 60: Support Method Chaining
  16. 7. Concurrency
    1. Item 61: Don’t Block the Event Queue on I/O
    2. Item 62: Use Nested or Named Callbacks for Asynchronous Sequencing
    3. Item 63: Be Aware of Dropped Errors
    4. Item 64: Use Recursion for Asynchronous Loops
    5. Item 65: Don’t Block the Event Queue on Computation
    6. Item 66: Use a Counter to Perform Concurrent Operations
    7. Item 67: Never Call Asynchronous Callbacks Synchronously
    8. Item 68: Use Promises for Cleaner Asynchronous Logic
  17. Index

7. Concurrency

JavaScript was designed as an embedded scripting language. JavaScript programs do not run as stand-alone applications, but as scripts in the context of a larger application. The flagship example is, of course, the web browser. A browser can have many windows and tabs running multiple web applications, each responding to various inputs and stimuli: user actions via keyboard, mouse, or touch, the arrival of data from the network, or timed alarms. These events can occur at any point—even simultaneously—during the lifetime of a web application. And for each kind of event, the application may wish to be notified of information and respond with custom behavior.

JavaScript’s approach to writing programs that respond to multiple concurrent ...

The best content for your career. Discover unlimited learning on demand for around $1/day.