Book Image

Learning Underscore.js

By : Alexandru Vasile Pop
Book Image

Learning Underscore.js

By: Alexandru Vasile Pop

Overview of this book

Table of Contents (14 chapters)
Learning Underscore.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 , 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 you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.