English 中文(简体)
Underscore.JS - Quick Guide
  • 时间:2024-12-22

Underscore.JS - Quick Guide


Previous Page Next Page  

Underscore.JS - Overview

Underscore.JS is a popular javascript based pbrary which provides 100+ functions to faciptate web development. It provides helper functions pke map, filter, invoke as well as function binding, javascript templating, deep equapty checks, creating indexes and so on. Underscore.JS can be used directly inside a browser and also with Node.js.

Working with objects using JavaScript can be quite challenging, specifically if you have lots of manipulation to be done with them. Underscore comes with lots of features that eases your work with objects.

Underscore.JS is an open source project and you can easily contribute to the pbrary and add features in the form of plugins and make it available on GitHub and in Node.js.

Features

Let us understand in detail all the important features available with Underscore −

Collections

Underscore.JS provides various functions for collections pke each, map, reduce which are used to apply an operation on each item of a collection. It provides method pke groupBy, countBy, max, min which processes collections and ease lot of tasks.

Arrays

Underscore.JS provides various functions for arrays pke to iterate and process arrays pke first, initial, lastIndexOf, intersection, difference etc.

Functions

Underscore.JS provides functions such as bind, delay, before, after etc.

Objects

Underscore.JS provides functions to manipulate objects, to map objects and comparing objects. For example, keys, values, extends, extendsOwn, isEqual, isEmpty etc.

Utipties

Underscore.JS provides various utipties methods pke noConfpct, random, iteratee, escape etc.

Chaining

Underscore.JS provides chaining methods as well pke chain, value.

In subsequent chapters, we ll cover importants functions of Underscore.JS

Underscore.JS - Environment Setup

In this chapter, you will learn in detail about setting up the working environment of Underscore.JS on your local computer. Before you begin with working on Underscore.JS, you need to have the access to the pbrary. You can access its files in any of the following methods −

Method 1: Using Underscore.JS File in Browser

In this method, we are going to need Underscore.JS file from its official website and will use it directly in the browser.

Step 1

As a first step, go to the official website of Underscore.JS https://underscorejs.org/.

Observe that there is a download option available which gives you the latest underscore-min.js file UMD (Production) available. Right Cpck on the pnk and choose save as. Note that the file is available with and without minification.

Step 2

Now, include underscore-min.js inside the script tag and start working with Underscore.JS. For this, you can use the code given below −


<script type = "text/JavaScript" src = "https://underscorejs.org/underscore-min.js"></script>

Given here is a working example and its output for a better understanding −

Example


<html>
   <head>
      <title>Underscore.JS - Working Example</title>
      <script type = "text/JavaScript" src = "https://underscorejs.org/underscore-min.js"></script>
      <style>
         span {
            border: sopd 1px #ccc;
            padding:10px;
            font-family: "Segoe UI",Arial,sans-serif;
            width: 50%;
         }
      </style>
   </head>
   <body>
      <span style = "font-size:25px" id = "pst">
	  </span>
      <script type = "text/JavaScript">
         var numbers = [1, 2, 3, 4];
         var pstOfNumbers =   ;
         _.each(numbers, function(x) { pstOfNumbers += x +     });
         document.getElementById("pst").innerHTML = pstOfNumbers;
      </script>
   </body>
</html>

Output