You are previewing JavaScript: Functional Programming for JavaScript Developers.
O'Reilly logo
JavaScript: Functional Programming for JavaScript Developers

Book Description

Unlock the powers of functional programming hidden within JavaScript to build smarter, cleaner, and more reliable web apps

About This Book

  • Write powerful code with the high-level functions that JavaScript offers

  • Discover what functional programming is, why it's effective, and how it's used in JavaScript

  • Understand and optimize JavaScript's hidden potential as a true functional language

  • Who This Book Is For

    If you are a JavaScript developer interested in learning functional programming, looking for the quantum leap toward mastering the JavaScript language, or just want to become a better programmer in general, then this book is ideal for you. This guide is aimed at programmers, involved in developing reactive frontend apps, server-side apps that wrangle with reliability and concurrency, and everything in between.

    What You Will Learn

  • Get a run through of the basic JavaScript language constructs

  • Code using the powerful object-oriented feature in JavaScript

  • Master DOM manipulation, cross-browser strategies, and ES6

  • Understand the basic concurrency constructs in Javascript and best performance strategies

  • Harness the power of patterns for tasks ranging from application building to code testing

  • Build large-scale apps seamlessly with the help of reactive patterns

  • Explore advanced design patterns, including dependency injection

  • Develop more powerful applications with currying and function composition

  • Create more reliable code with closures and immutable data

  • In Detail

    JavaScript is a high-level, dynamic, untyped, lightweight, and interpreted programming language and functional programming is a style that emphasizes and enables smarter code that minimizes complexity and increases modularity. It's a way of writing cleaner code through clever ways of mutating, combining, and using functions. And JavaScript provides an excellent medium for this approach. By learning how to expose JavaScript's true identity as a functional language, we can implement web apps that are more powerful, easier to maintain and more reliable.

    The java script: Functional Programming for JavaScript Developers course will take you on a journey to show how functional programming when combined with other techniques makes JavaScript programming more efficient. The first module Mastering JavaScript, stress on practical aspects of Javascript development like-Functions and Closures, Runtime debugging techniques, project layout, events and DOM processing, build tools, Object-oriented patterns, isomorphism-everything that a modern Javascript project would need.

    The second module, Mastering JavaScript Design Patterns - Second Edition, will explore how design patterns can help you improve and organize your JavaScript code. You'll get to grips with creational, structural, and behavioral patterns as you discover how to put them to work in different scenarios. This updated edition will also delve into reactive design patterns and microservices as they are a growing phenomenon in the world of web development. It will also show you some advanced patterns, including dependency injection and live post processing.

    The third module, Functional Programming in JavaScript, will help you to write real-world applications by utilizing a wide range of functional techniques and styles. It explores the core concepts of functional programming common to all functional languages, with examples of their use in JavaScript.

    Style and approach

    This course will begin with providing insights and practical tips on advanced JavaScript features to build highly scalable web and mobile system and move on to some design patterns with JavaScript. Finally, the course ends with presenting the functional programming techniques and styles in JavaScript.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. JavaScript: Functional Programming for JavaScript Developers
      1. Table of Contents
      2. JavaScript: Functional Programming for JavaScript Developers
      3. JavaScript: Functional Programming for JavaScript Developers
      4. Credits
      5. Preface
        1. What this learning path covers
        2. What you need for this learning path
        3. Who this learning path is for
        4. Reader feedback
        5. Customer support
          1. Downloading theexample code
          2. Errata
          3. Piracy
          4. Questions
      6. 1. Module 1
        1. 1. JavaScript Primer
          1. A little bit of history
          2. How to use this book
          3. Hello World
            1. An overview of JavaScript
              1. Comments
              2. Variables
              3. Constants
              4. Number
              5. String
              6. Undefined values
              7. Booleans
              8. The instanceof operator
              9. Date objects
              10. The + operator
              11. The ++ and -- operators
              12. Boolean operators
              13. Equality
                1. Strict equality using ===
                2. Weak equality using ==
            2. JavaScript types
            3. Automatic semicolon insertion
            4. JavaScript style guide
              1. Whitespaces
              2. Parentheses, line breaks, and braces
              3. Quotes
              4. End of lines and empty lines
              5. Type checking
              6. Type casting
              7. Conditional evaluation
              8. Naming
              9. The eval() method is evil
              10. The strict mode
                1. Enabling the strict mode for an existing code can break it
                2. Package with care
                3. Variables must be declared in strict mode
                  1. The eval() function is cleaner in strict mode
                4. Features that are blocked in strict mode
              11. Running JSHint
          4. Summary
        2. 2. Functions, Closures, and Modules
          1. A function literal
            1. A function declaration
          2. Functions as data
          3. Scoping
            1. Global scope
            2. Local scope
            3. Function-level scope versus block-level scope
            4. Inline function expressions
            5. Block scopes
          4. Function declarations versus function expressions
          5. The arguments parameter
            1. The this parameter
              1. Invocation as a function
              2. Invocation as a method
              3. Invocation as a constructor
              4. Invocation using apply() and call() methods
          6. Anonymous functions
            1. Anonymous functions while creating an object
            2. Anonymous functions while creating a list
            3. Anonymous functions as a parameter to another function
            4. Anonymous functions in conditional logic
          7. Closures
          8. Timers and callbacks
          9. Private variables
          10. Loops and closures
          11. Modules
            1. Stylistic considerations
          12. Summary
        3. 3. Data Structures and Manipulation
          1. Regular expressions
          2. Exact match
          3. Match from a class of characters
          4. Repeated occurrences
            1. Alternatives – OR
          5. Beginning and end
          6. Backreferences
          7. Greedy and lazy quantifiers
          8. Arrays
          9. Maps
          10. Sets
          11. A matter of style
          12. Summary
        4. 4. Object-Oriented JavaScript
          1. Understanding objects
            1. Behavior of JavaScript objects
            2. Prototypes
          2. Instance properties versus prototype properties
          3. Inheritance
          4. Getters and setters
          5. Summary
        5. 5. Testing and Debugging
          1. Unit testing
            1. Test-driven development
            2. Behavior-driven development
          2. JavaScript debugging
            1. Syntax errors
            2. Using strict
            3. Runtime exceptions
              1. Console.log and asserts
              2. Chrome DevTools
          3. Summary
        6. 6. ECMAScript 6
          1. Shims or polyfills
          2. Transpilers
          3. ES6 syntax changes
            1. Block scoping
            2. Default parameters
            3. Spread and rest
            4. Destructuring
            5. Object literals
            6. Template literals
            7. Maps and Sets
            8. Symbols
            9. Iterators
            10. For..of loops
            11. Arrow functions
          4. Summary
        7. 7. DOM Manipulation and Events
          1. DOM
            1. Accessing DOM elements
            2. Accessing specific nodes
          2. Chaining
          3. Traversal and manipulation
          4. Working with browser events
          5. Propagation
          6. jQuery event handling and propagation
          7. Event delegation
          8. The event object
          9. Summary
        8. 8. Server-Side JavaScript
          1. An asynchronous evented-model in a browser
          2. Callbacks
          3. Timers
          4. EventEmitters
          5. Modules
            1. Creating modules
          6. npm
            1. Installing packages
          7. JavaScript performance
            1. JavaScript profiling
              1. The CPU profile
              2. The Timeline view
          8. Summary
      7. 2. Module 2
        1. 1. Designing for Fun and Profit
          1. The road to JavaScript
            1. The early days
            2. A pause
            3. The way of GMail
            4. JavaScript everywhere
          2. What is a design pattern?
          3. Anti-patterns
          4. Summary
        2. 2. Organizing Code
          1. Chunks of code
          2. What's the matter with global scope anyway?
          3. Objects in JavaScript
          4. Build me a prototype
          5. Inheritance
          6. Modules
          7. ECMAScript 2015 classes and modules
          8. Best practices and troubleshooting
          9. Summary
        3. 3. Creational Patterns
          1. Abstract factory
            1. Implementation
          2. Builder
            1. Implementation
          3. Factory method
            1. Implementation
          4. Singleton
            1. Implementation
            2. Disadvantages
          5. Prototype
            1. Implementation
          6. Tips and tricks
          7. Summary
        4. 4. Structural Patterns
          1. Adapter
            1. Implementation
          2. Bridge
            1. Implementation
          3. Composite
            1. Example
            2. Implementation
          4. Decorator
            1. Implementation
          5. Façade
            1. Implementation
          6. Flyweight
            1. Implementation
          7. Proxy
            1. Implementation
          8. Hints and tips
          9. Summary
        5. 5. Behavioral Patterns
          1. Chain of responsibility
            1. Implementation
          2. Command
            1. Command message
            2. Invoker
            3. Receiver
          3. Interpreter
            1. Example
            2. Implementation
          4. Iterator
            1. Implementation
            2. ECMAScript 2015 iterators
          5. Mediator
            1. Implementation
          6. Memento
            1. Implementation
          7. Observer
            1. Implementation
          8. State
            1. Implementation
          9. Strategy
            1. Implementation
          10. Template method
            1. Implementation
          11. Visitor
          12. Hints and tips
          13. Summary
          14. Part 2
        6. 6. Functional Programming
          1. Functional functions are side-effect-free
          2. Function passing
            1. Implementation
          3. Filters and pipes
            1. Implementation
          4. Accumulators
            1. Implementation
          5. Memoization
            1. Implementation
          6. Immutability
          7. Lazy instantiation
            1. Implementation
          8. Hints and tips
          9. Summary
        7. 7. Reactive Programming
          1. Application state changes
          2. Streams
          3. Filtering streams
          4. Merging streams
          5. Streams for multiplexing
          6. Hints and tips
          7. Summary
        8. 8. Application Patterns
          1. First, some history
          2. Model View Controller
            1. MVC code
          3. Model View Presenter
            1. MVP code
          4. Model View ViewModel
            1. MVVM code
            2. A better way to transfer changes between the model and the view
            3. Observing view changes
          5. Tips and tricks
          6. Summary
        9. 9. Web Patterns
          1. Sending JavaScript
            1. Combining files
            2. Minification
            3. Content Delivery Networks
          2. Plugins
            1. jQuery
            2. d3
          3. Doing two things at once – multithreading
          4. Circuit breaker pattern
            1. Back-off
            2. Degraded application behavior
          5. Promise pattern
          6. Hints and tips
          7. Summary
        10. 10. Messaging Patterns
          1. What's a message anyway?
            1. Commands
            2. Events
          2. Request-reply
          3. Publish-subscribe
            1. Fan out and in
          4. Dead letter queues
            1. Message replay
            2. Pipes and filters
            3. Versioning messages
          5. Hints and tips
          6. Summary
        11. 11. Microservices
          1. Façade
          2. Service selector
          3. Aggregate services
          4. Pipeline
          5. Message upgrader
          6. Failure patterns
            1. Service degradation
            2. Message storage
            3. Message replay
            4. Indempotence of message handling
          7. Hints and tips
          8. Summary
        12. 12. Patterns for Testing
          1. The testing pyramid
          2. Testing in the small with unit tests
          3. Arrange-Act-Assert
            1. Assert
          4. Fake objects
          5. Test spies
          6. Stubs
          7. Mock
          8. Monkey patching
          9. Interacting with the user interface
            1. Browser testing
            2. Faking the DOM
            3. Wrapping the manipulation
          10. Tips and tricks
          11. Summary
        13. 13. Advanced Patterns
          1. Dependency injection
          2. Live post processing
          3. Aspect oriented programming
          4. Mixins
          5. Macros
          6. Tips and tricks
          7. Summary
        14. 14. ECMAScript-2015/2016 Solutions Today
          1. TypeScript
            1. Decorators
            2. Async/Await
            3. Typing
          2. BabelJS
            1. Classes
            2. Default parameters
            3. Template literals
            4. Block bindings with let
            5. In production
          3. Tips and tricks
          4. Summary
      8. 3. Module 3
        1. 1. The Powers of JavaScript's Functional Side – a Demonstration
          1. Introduction
          2. The demonstration
          3. The application – an e-commerce website
            1. Imperative methods
          4. Functional programming
          5. Summary
        2. 2. Fundamentals of Functional Programming
          1. Functional programming languages
            1. What makes a language functional?
            2. Advantages
              1. Cleaner code
              2. Modularity
              3. Reusability
              4. Reduced coupling
              5. Mathematically correct
            3. Functional programming in a nonfunctional world
              1. Is JavaScript a functional programming language?
          2. Working with functions
            1. Self-invoking functions and closures
            2. Higher-order functions
            3. Pure functions
            4. Anonymous functions
            5. Method chains
            6. Recursion
              1. Divide and conquer
            7. Lazy evaluation
          3. The functional programmer's toolkit
            1. Callbacks
            2. Array.prototype.map()
            3. Array.prototype.filter()
            4. Array.prototype.reduce()
            5. Honorable mentions
              1. Array.prototype.forEach
              2. Array.prototype.concat
              3. Array.prototype.reverse
              4. Array.prototype.sort
              5. Array.prototype.every and Array.prototype.some
          4. Summary
        3. 3. Setting Up the Functional Programming Environment
          1. Introduction
          2. Functional libraries for JavaScript
            1. Underscore.js
            2. Fantasy Land
            3. Bilby.js
            4. Lazy.js
            5. Bacon.js
            6. Honorable mentions
          3. Development and production environments
            1. Browsers
            2. Server-side JavaScript
              1. A functional use case in the server-side environment
            3. CLI
            4. Using functional libraries with other JavaScript modules
            5. Functional languages that compile into JavaScript
          4. Summary
        4. 4. Implementing Functional Programming Techniques in JavaScript
          1. Partial function application and currying
            1. Function manipulation
              1. Apply, call, and the this keyword
              2. Binding arguments
              3. Function factories
            2. Partial application
              1. Partial application from the left
              2. Partial application from the right
            3. Currying
          2. Function composition
            1. Compose
              1. Sequence – compose in reverse
            2. Compositions versus chains
            3. Programming with compose
          3. Mostly functional programming
            1. Handling events
          4. Functional reactive programming
            1. Reactivity
            2. Putting it all together
          5. Summary
        5. 5. Category Theory
          1. Category theory
            1. Category theory in a nutshell
            2. Type safety
              1. Object identities
          2. Functors
            1. Creating functors
            2. Arrays and functors
            3. Function compositions, revisited
          3. Monads
            1. Maybes
            2. Promises
            3. Lenses
            4. jQuery is a monad
          4. Implementing categories
          5. Summary
        6. 6. Advanced Topics and Pitfalls in JavaScript
          1. Recursion
            1. Tail recursion
              1. The Tail-call elimination
            2. Trampolining
            3. The Y-combinator
              1. Memoization
          2. Variable scope
            1. Scope resolutions
              1. Global scope
              2. Local scope
              3. Object properties
            2. Closures
            3. Gotchas
          3. Function declarations versus function expressions versus the function constructor
            1. Function declarations
            2. Function expressions
            3. The function constructor
            4. Unpredictable behavior
          4. Summary
        7. 7. Functional and Object-oriented Programming in JavaScript
          1. JavaScript – the multi-paradigm language
          2. JavaScript's object-oriented implementation – using prototypes
            1. Inheritance
            2. JavaScript's prototype chain
            3. Inheritance in JavaScript and the Object.create() method
          3. Mixing functional and object-oriented programming in JavaScript
            1. Functional inheritance
              1. Strategy Pattern
            2. Mixins
              1. Classical mixins
              2. Functional mixins
          4. Summary
        8. A. Common Functions for Functional Programming in JavaScript
        9. B. Glossary of Terms
      9. A. Bibliography
      10. Index