You are previewing Learning Underscore.js.
O'Reilly logo
Learning Underscore.js

Book Description

Explore the Underscore.js library by example using a test-driven development approach

About This Book

  • Understand and learn to apply functional programming principles using the built-in functions of Underscore.js

  • Leverage and reuse Underscore.js-based code to create code that targets client, server, or database contexts

  • Take Underscore.js further by reusing code between client and server and by learning about other closely related libraries

  • Who This Book Is For

    If you are a developer with fundamental JavaScript knowledge and want to use modern JavaScript libraries to extend your functional programming skills, then Underscore.js is an important library you should be familiar with.

    What You Will Learn

  • Reference and call Underscore.js functions using a modern JavaScript development workflow

  • Apply Underscore.js to JavaScript arrays, objects, and functions

  • Take advantage of object-oriented or functional programming techniques with Underscore.js

  • Leverage Underscore.js to create code that targets client, server, or database contexts

  • Extend Underscore.js functionality with other closely related libraries

  • Reuse Underscore.js-based code between client and server applications

  • Prepare for the upcoming JavaScript standard ECMAScript 6 and support older browsers

  • In Detail

    Underscore.js is one of the most popular modern JavaScript libraries used for functional programming. It can be used as a base for building complex JavaScript applications in a sustainable manner and for building other JavaScript libraries. It embraces functional programming principles but is not opinionated and can be used with imperative, object-oriented, functional, or other programming styles.

    This book explores how to use Underscore.js to power your code and understand modern JavaScript development concepts while applying a lightweight and efficient workflow to build applications.

    The book starts with an incremental Underscore.js introduction by exploring key JavaScript concepts. You will then explore the basic features of Underscore.js in action and establish a lightweight development workflow that allows the provided examples to be guided by tests. The book then covers the functionality of Underscore.js with in-depth examples and explanations for understanding and applying the Underscore.js API. You'll also learn how to use Underscore.js as a base for your own modules and libraries within an object-oriented or functional programming style, and will be able to explore Underscore.js use cases in different environments.

    Eventually, you'll learn about libraries that are closely related with Underscore.js, how to share code between client and server, and how to prepare for the upcoming JavaScript standard ECMAScript 6.

    Style and approach

    This book takes an example-driven approach to describing some of the essential JavaScript concepts and practices that are useful for building sustainable applications.

    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. Learning Underscore.js
      1. Table of Contents
      2. Learning Underscore.js
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with Underscore.js
        1. Why Underscore
        2. Getting started with Underscore by example
          1. The ECMAScript 5 starter example
          2. The Underscore find starter example
          3. The Underscore countBy starter example
        3. Key Underscore functions
          1. Underscore each
          2. Underscore map and reduce
        4. Functional programming fundamentals
        5. Useful patterns and practices for JavaScript applications targeting ECMAScript 5
          1. The immediately-invoked function expression
          2. The revealing module pattern
          3. The JavaScript strict mode
        6. Setting up a development workflow for exploring Underscore
          1. Modern JavaScript development with Node.js
            1. Windows
            2. Mac OS X
            3. Linux
            4. Verifying Node.js installation
          2. Managing JavaScript dependencies with Bower
          3. Choosing a JavaScript editor
        7. Testing JavaScript code with Jasmine
          1. Jasmine introduction
          2. Adding tests using the default Jasmine infrastructure
        8. Summary
      9. 2. Using Underscore.js with Collections
        1. Key Underscore functions revisited – each, map, and reduce
          1. Applying reflection techniques
          2. Manipulating the this variable
          3. Using map and reduce with object properties
        2. Searching and filtering
          1. Searching
            1. Underscore find
            2. Underscore some
            3. Underscore findWhere
            4. Underscore contains
          2. Filtering
            1. Underscore filter
            2. Underscore where
            3. Underscore reject and partition
            4. Underscore every
        3. Aggregations and transformations
          1. Aggregations
            1. Underscore max and min
          2. Transformations
            1. Underscore sortBy
            2. Underscore groupBy
            3. Underscore indexBy
            4. Underscore countBy
        4. Other collection-based functions
        5. Summary
      10. 3. Using Underscore.js with Arrays, Objects, and Functions
        1. Arrays
          1. Extracting array start and end sequences
          2. Union, intersect, and related functions
          3. Other array-related functions
        2. Objects
          1. Underscore keys
          2. Underscore values and pairs
          3. Underscore invert and functions
          4. Underscore pick, omit, and has
          5. Underscore extend, clone, and defaults
          6. Underscore has, property, propertyOf, and matcher
          7. Comparing objects and making assertions against objects
          8. Other object-related functions
        3. Functions
          1. Functional composition with bind, bindAll, and partial
          2. Further functional composition with memoize, wrap, negate, and compose
          3. Controlling when and how often functions are called
        4. Utility functions
        5. Summary
      11. 4. Programming Paradigms with Underscore.js
        1. The object-oriented programming paradigm
          1. Inheritance with object literals
          2. Inheritance with object constructors
          3. Using Underscore with the object-oriented programming paradigm
            1. Using classes with original client data
            2. Constructor validation with Underscore
        2. The functional programming paradigm
        3. Migrating to a functional programming style
        4. Functional programming with Underscore.js
        5. Summary
      12. 5. Using Underscore.js in the Browser, on the Server, and with the Database
        1. Using Underscore in the browser
        2. Enhancing examples output with Bootstrap
          1. Better HTML markup with Underscore templates
        3. Using Underscore on the server with Node.js
          1. Executing JavaScript with Node.js
          2. Using Node.js modules
          3. Locating modules
          4. Creating a npm package
          5. Converting JavaScript code to Node.js modules
          6. Running tests with Node.js
        4. Using Underscore with MongoDB
          1. Installing and configuring MongoDB
            1. Installing MongoDB on Windows
            2. Installing MongoDB on Ubuntu Linux
            3. Installing MongoDB on Mac OS X
            4. Configuring and running MongoDB
          2. Creating initial data using the MongoDB client and Underscore
          3. Asynchronous programming in Node.js
          4. Accessing data using the MongoDB Node.js driver
        5. Using Underscore with PostgreSQL
          1. Installing and configuring PostgreSQL
            1. Installing PostgreSQL on Windows
            2. Installing PostgreSQL on Ubuntu Linux
            3. Installing PostgreSQL on Mac OS X
          2. Using psql with basic database commands
          3. PostgreSQL data types
            1. SQL types
            2. The jsonb type
          4. Using PostgreSQL with plv8
          5. Generating data using plv8 and Underscore
        6. Summary
      13. 6. Related Underscore.js Libraries and ECMAScript Standards
        1. Using the Underscore-contrib library
          1. An overview of Underscore-contrib functionality
          2. Exploring Underscore-contrib via examples
        2. Using the lodash library
          1. An overview of lodash functionality
          2. Migrating a project from Underscore to lodash
        3. Underscore and JavaScript standards
          1. ECMAScript 5.1 (ES5)
          2. ECMAScript 2015 (ES6)
            1. Array – new features
            2. Other notable new features
            3. Using ECMAScript 2015 (ES6) today with transpilers
              1. Using Babel directly in the browser
              2. An overview of Babel CLI
              3. Using Babel CLI for the client-side code
              4. Using Babel with Node.js
            4. ECMAScript 2015 (ES6) by example
              1. Testing ES6 code
              2. Further ES6 examples – the arrow function
              3. Further ES6 examples – function declaration improvements
        4. Summary
      14. 7. Underscore.js Build Automation and Code Reusability
        1. Build automation with Gulp
        2. Reusing code based on Underscore between client and server
          1. Using CommonJS modules for packaging client code with Browserify
          2. Testing CommonJS modules in the browser with Browserify
          3. Using Gulp with Browserify
        3. Adding ECMAScript 2015 (ES6) support to Browserify
        4. Summary
      15. Index