Learning Underscore.js
eBook - ePub

Learning Underscore.js

  1. 224 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learning Underscore.js

About this book

Explore the Underscore.js library by example using a test-driven development approachAbout 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 ForIf 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 browsersIn DetailUnderscore.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 approachThis book takes an example-driven approach to describing some of the essential JavaScript concepts and practices that are useful for building sustainable applications.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Learning Underscore.js


Table of Contents

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

Learning Underscore.js

Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: October 2015
Production reference: 1261015
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-381-6
www.packtpub.com

Credits

Author
Alex Pop
Reviewers
Craig Davis
Pavel Tkachenko
Jitendra Zaa
Commissioning Editor
Dipika Gaonkar
Acquisition Editors
Indrajit Das
Rebecca Pedley
Content Development Editor
Divij Kotian
Technical Editor
Siddhi Rane
Copy Editor
Janbal Dharmaraj
Project Coordinator
Nikhil Nair
Proofreader
Safis Editing
Indexer
Hemangini Bari
Production Coordinator
Komal Ramchandani
Cover Work
Komal Ramchandani

About the Author

Alex Pop is a professional software developer with 14 years of experience in building applications for various platforms and technologies.
He has worked for ISVs, building enterprise resource planning applications, a content management system, and insurance and financial software products, and is currently working in the higher education sector as a web application developer.
He is the author of Learning AngularJS for .NET Developers, Packt Publishing. His developer blog at http://alexvpop.blogspot.co.uk/ contai...

Table of contents

  1. Learning Underscore.js

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Learning Underscore.js by Alex Pop in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in JavaScript. We have over one million books available in our catalogue for you to explore.