Learning Underscore.js
By Pop Alex
()
About this ebook
Related to Learning Underscore.js
Related ebooks
Mastering MeteorJS Application Development Rating: 4 out of 5 stars4/5Android Application Development with Maven Rating: 0 out of 5 stars0 ratingsOpa Application Development Rating: 0 out of 5 stars0 ratingsjQuery Design Patterns Rating: 0 out of 5 stars0 ratingsBuilding Impressive Presentations with Impress.js Rating: 4 out of 5 stars4/5Jasmine JavaScript Testing - Second Edition Rating: 0 out of 5 stars0 ratingsAdvanced Express Web Application Development Rating: 0 out of 5 stars0 ratingsMastering Web Application Development with Express Rating: 0 out of 5 stars0 ratingsBuilding React Apps with Server-Side Rendering: Use React, Redux, and Next to Build Full Server-Side Rendering Applications Rating: 0 out of 5 stars0 ratingsAsynchronous Android Programming - Second Edition Rating: 0 out of 5 stars0 ratingsNode.js: Novice to Ninja Rating: 0 out of 5 stars0 ratingsAmazon SimpleDB: LITE Rating: 0 out of 5 stars0 ratingsRedmine Plugin Extension and Development Rating: 0 out of 5 stars0 ratingsAngularJS Web Application Development Blueprints Rating: 0 out of 5 stars0 ratingsReact Components Rating: 0 out of 5 stars0 ratingsReSharper Essentials Rating: 4 out of 5 stars4/5NW.js Essentials Rating: 0 out of 5 stars0 ratingsDeveloping Web Services with Java APIs for XML Using WSDP Rating: 0 out of 5 stars0 ratingsInstant Highcharts Rating: 0 out of 5 stars0 ratingsLocation-Aware Applications Rating: 0 out of 5 stars0 ratingsSoftware architecture A Complete Guide - 2019 Edition Rating: 0 out of 5 stars0 ratingsFlex 3 with Java Rating: 0 out of 5 stars0 ratingsLucene 4 Cookbook Rating: 0 out of 5 stars0 ratingsWebRTC Blueprints Rating: 0 out of 5 stars0 ratingsFlex on Java Rating: 0 out of 5 stars0 ratingsPHP Web 2.0 Mashup Projects: Practical PHP Mashups with Google Maps, Flickr, Amazon, YouTube, MSN Search, Yahoo! Rating: 0 out of 5 stars0 ratingsAJAX Interview Questions, Answers, and Explanations: AJAX Certification Review Rating: 0 out of 5 stars0 ratingsSharePoint 2010 Web Parts in Action Rating: 0 out of 5 stars0 ratings
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Game Development with Unreal Engine 5: Learn the Basics of Game Development in Unreal Engine 5 (English Edition) Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsThe Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsLearn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsLinux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Web Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5
Reviews for Learning Underscore.js
0 ratings0 reviews
Book preview
Learning Underscore.js - Pop Alex
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
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/ contains technical articles about .NET, JavaScript, and various software engineering topics.
I would like to thank my wife and daughter for their support and understanding.
About the Reviewers
Craig Davis is a software engineer with a 20-year history in the web industry. He works at Help Scout, an organization that helps people bring personalized e-mail support to the enterprise. He has been a technical editor for many programming books and is proud to contribute to multiple open source projects. He is a competitive cyclist and runner and lives in New Mexico with his wife and their dog.
Pavel Tkachenko is an inspired self-taught computer wizard. Since childhood, he has had a passion for designing and developing websites and maintaining complex server architectures. He has created a number of original tools, such as HTMLki, Sqobot, and Sqimitive, to tackle common problems in a new way. He is also the founder of the Russian Laravel community and an active member of Russian publication networks such as Habrahabr.ru.
He has been freelancing since 2009, working on all sorts of websites built around PHP, Rails, Python, JavaScript, and MySQL. He used to lead his own team as well as work on his own. His views are often unconventional, but that's what allows him to work on high-profile projects with a custom approach. You can contact him via his page at http://proger.me.
Jitendra Zaa is Salesforce MVP with more than 6 years of experience in web technologies and cloud platforms. He is a manager at Cognizant technology solutions.
You can follow him @JitendraZaa or at his website http://jitendrazaa.com/.
Writing technical blog articles, learning new programming languages and frameworks, and sharing knowledge with others are some of his hobbies.
His experience and projects normally include Salesforce, Java, C#, ASP.Net, JIRA, and PHP-based applications.
Other books he has reviewed include Force.com Enterprise Architecture, Developing Applications with Salesforce Chatter, and Visualforce Development Cookbook, all by Packt Publishing. Right now, he is writing a book on design patterns in Salesforce with Packt Publishing.
I would like to thank my mom, dad, my wife, Minal, and our adorable son, Rudra, for all their support, time, and above all for the motivation to go on.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Preface
Over the last few years, JavaScript has emerged as one the most popular programming languages, expanding its reach from its original browser environment to server runtimes, databases, and to mobile, embedded, or desktop applications. The sixth edition of its specification ECMAScript 2015 (ES6) was a major milestone that brought JavaScript features in line with other popular programming languages. The community and the browser vendors stepped up their efforts to support ES6, and we should see full support for the standard across multiple JavaScript engines as soon as early 2016. The future of JavaScript beyond ES6 looks very promising as its standardization effort aims to release a new specification every year.
In summer 2015, JavaScript was the most popular language used on the GitHub repository hosting service and had the biggest number of packages compared to all other languages. This change in popularity was made possible by an intense process of creation and refinement of libraries and frameworks alongside an improvement in development standards and practices throughout the JavaScript community. One of these modern JavaScript libraries is jQuery, the most popular browser library, which was a great facilitator in speeding up the adoption of common web standards across browser engines. Similarly, at least in impact, the Underscore library brought uniformity and functional programming features to JavaScript while bridging the gap between client and server environments.
Underscore represents a model of code quality and community involvement that other libraries should try to emulate, and many libraries were inspired or built upon Underscore. Throughout this book, Underscore proved to be the perfect vehicle to explore programming concepts that apply to any type of application. It is also a great example for the concept of universal JavaScript where the same code can be executed in multiple environments. For a JavaScript developer, Underscore is the JavaScript-based library that can be used to create code that runs everywhere or as a foundation for adopting a functional programming style.
JavaScript has become an important language in enterprise software development with the adoption of the Node.js runtime by companies such as Microsoft, IBM, and Intel to name just a few. The considerable rise in popularity of single page application frameworks, such as Backbone.js, AngularJS, React, and many others, means that developers are writing more complex JavaScript code with an increased effort in managing the increase in complexity. Adopting standards such as ES6 and using Underscore or similar libraries should help in coping with the challenges of the rapidly and continuously changing industry.
What this book covers
Chapter 1, Getting Started with Underscore.js, introduces you to Underscore and explains the main problems addressed by the library together with a quick introduction to functional programming. This chapter describes some of the concepts and patterns that are used by Underscore or are helpful when using Underscore. The final part is a walkthrough to set up the development environment used throughout the book with a starter example.
Chapter 2, Using Underscore.js with Collections, explores the Underscore functionality for collections by example and explains some of the concepts related to these areas—scope resolution and execution context, map/reduce, and functional programming concepts.
Chapter 3, Using Underscore.js with Arrays, Objects, and Functions, looks at functionality for arrays, objects, and functions and builds upon the concepts and functions introduced in the previous chapter. In the functions section, you will learn how to manipulate function scopes and arguments and why this is an important aspect of JavaScript programming in general.
Chapter 4, Programming Paradigms with Underscore.js, expands on the concepts and practices of functional programming to solve common programming problems. The chapter starts with a differentiation between object-oriented programming and functional programming. It continues by exploring examples of functional programming with Underscore.
Chapter 5, Using Underscore.js in the Browser, on the Server, and with the Database, is about using Underscore in specific contexts, starting with the browser environment, closely followed by Node.js server-side applications or libraries. You will then learn how to use Underscore with databases such as MongoDB and PostgreSQL, which can execute JavaScript to query data.
Chapter 6, Related Underscore.js Libraries and ECMAScript Standards, discusses advanced topics such as the link between Underscore and JavaScript standards, libraries that augment or can even replace Underscore, with a focus on taking advantage of the new ECMAScript 2015 (ES6) features.
Chapter 7, Underscore.js Build Automation and Code Reusability, introduces a build automation solution together with strategies to reuse Underscore based code between different application hosts.
What you need for this book
You need to have the Node.js runtime installed and it is available at https://nodejs.org/. Details on how to install it can be found in Chapter 1, Getting Started with Underscore.js.
All the code in this book can be edited in any text editor or IDE that you are familiar with and has support for the JavaScript language.
Who this book is for
This book is for developers with fundamental JavaScript knowledge who want to use modern JavaScript libraries to advance their programming skills. Underscore is one of the most important libraries you should be familiar with and the book will help you achieve this goal by going through its fundamentals and using it in a wide variety of contexts. This book should be relevant to anyone who:
Is interested in building web applications, single-page web applications or JavaScript-based desktop, mobile, or embedded applications
Wants to use Node.js to build web applications or web services
Wants to work with databases such as MongoDB or PostgreSQL and leverage their JavaScript support
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: We just need to open the SpecRunner.html file in a browser and we should see this output.
A block of code is set as follows:
var peopleWithAwardAge = _.map(people, function(person){
return {
name: person.name,
awardAge: person.awardYear - person.birthYear
}
});
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
var peopleWithAwardAge = _.map(people, function(person){
return {
name: person.name,
awardAge: person.awardYear - person.birthYear
}
});
Any command-line input or output is written as follows:
npm install -g bower
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: Executing the following line in a Command Prompt with administrator privileges (opened using the Run as administrator option).
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <feedback@packtpub.com>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If