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

Why Underscore


In the last couple of years, the JavaScript programming language has extended its reach dramatically. While initially it was a browser-based scripting language, it is now used in server-side applications via platforms such as Node.js or in mobile and desktop applications via frameworks such as PhoneGap and Node-Webkit. Database engines such as MongoDB and PostgreSQL also use JavaScript, and this makes it possible to write an application using the same programming language throughout all its layers and components. Another significant change that raised the importance of JavaScript was the emergence of libraries and frameworks that targeted the browser as an application platform. Libraries such as jQuery enabled cross-browser HTML element manipulation, among other features, and frameworks such as Backbone.js facilitated building single page applications.

Note

A single page application (also known as SPA) has the user interface rendering and navigation happening in the browser rather than on the server.

The library presented in this book is called Underscore and provides an extensive set of functions that simplify and enhance handling of JavaScript objects, arrays, and functions. It accomplishes this by providing missing functional programming features to JavaScript. By using Underscore JavaScript gains a better level of usability that makes coding easier and more expressive on a similar level to other general purpose programming languages.

Version 1.0 of Underscore was launched in 2010 around the time when single page applications started to gain more ground. Underscore is used in Backbone.js as both have the same author, and after its launch it has become one of the most popular JavaScript libraries. At the time of writing this book, Underscore has reached version 1.8.3, which will be the version used throughout all examples.

Underscore is representative of a good JavaScript utility library as it provides solutions for a specific problem domain rather than being a catchall utility. It also has very good online documentation (including annotated source code) that is available at http://underscorejs.org/, which is another distinctive attribute of a good software library. This book will explore Underscore by presenting a series of more involved examples than the ones provided with its library documentation.

To understand why Underscore is so popular, we need to discuss the ECMAScript 5 (ES5) specification and what it meant for JavaScript as a programming language. Technically, JavaScript is a specific implementation of an open language specification called ECMAScript. The current version of this specification was finalized at the end of 2009 and is known as ECMAScript 5 (or ECMAScript 5.1 to be very specific). This version added functionality for the built-in JavaScript objects, Array and Object, included new functional features, and improved the meta-programming story among other changes. Soon after its release, it started to be adopted by all major browsers, including Internet Explorer from version 9. There was still a large number of users relying on browsers such as Internet Explorer 6, 7, and 8 that were unlikely to upgrade too quickly to Internet Explorer 9 compared to users of browsers such as Mozilla Firefox and Google Chrome that had faster release and upgrade cycles. As Underscore provided support for some of the functionality introduced by ECMAScript 5, this made it a useful library for web applications targeting older browsers such as Internet Explorer 8, and for developers that wanted to write code that was based on ES5 without worrying about browser support. Although ES5 support is important, this is just a small feature of Underscore compared to the rest of its features.

All of this book's examples assume that they are executed against a JavaScript version that is ES5 compliant. All the examples can be executed on Windows, Mac OS X, and Linux, and we will mainly target Google Chrome and Mozilla Firefox that are the most popular cross platform browsers (although you should not have issues running the examples in other browsers).

Note

Technically, Underscore is not directly compatible with ES5 starting from version 1.7.0, and we will discuss more about standards compliance in Chapter 6, Related Underscore.js Libraries and ECMAScript Standards.

ES 5 support for older browsers should be provided through a library targeting this feature exclusively such as es5-shim. This library is available at https://github.com/es-shims/es5-shim, where you can find more details about its features.