Home >Web Front-end >JS Tutorial >Summary of common methods of Underscore.js_Others
Overview
Underscore.js is a very lean library, only 4KB compressed. It provides dozens of functional programming methods, which greatly facilitates Javascript programming. The MVC framework backbone.js is based on this library.
It defines an underscore (_) object, and all methods of the function library belong to this object. These methods can be roughly divided into five categories: collection, array, function, object and utility.
Install under node.js
Underscore.js can be used not only in browser environments, but also in node.js. The installation command is as follows:
Methods related to collections
Javascript language data collection includes two structures: arrays and objects. The following methods apply to both structures.
map
This method performs some operation on each member of the collection in turn, and stores the returned value in a new array.
each
This method is similar to map, which performs some operation on each member of the collection in turn, but does not return a value.
reduce
This method performs some kind of operation on each member of the set in turn, and then accumulates the operation results on a certain initial value. After all operations are completed, the accumulated value is returned.
This method accepts three parameters. The first parameter is the collection being processed, the second parameter is the function that operates on each member, and the third parameter is the variable used for accumulation.
_.reduce([1, 2, 3], function(memo, num){ return memo num; }, 0); // 6
The second parameter of the reduce method is the operation function, which itself accepts two parameters. The first is the variable used for accumulation, and the second is the value of each member of the set.
filter and reject
The filter method performs some operation on each member of the collection in turn, and only returns members whose operation result is true.
every and some
The every method performs some operation on each member of the collection in turn. If the operation results of all members are true, it returns true, otherwise it returns false.
find
This method performs some operation on each member of the collection in turn and returns the first member whose operation result is true. If the operation result of all members is false, undefined is returned.
contains
If a value is in the collection, this method returns true, otherwise it returns false.
countBy
This method performs some kind of operation on each member of the set in turn, counts members with the same operation results as one category, and finally returns an object indicating the number of members corresponding to each operation result.
shuffle
This method returns a shuffled collection.
size
This method returns the number of members of the collection.
Methods related to objects
toArray
This method converts the object into an array.
pluck
This method extracts the value of a certain attribute of multiple objects into an array.
Methods related to functions
bind
This method binds the function runtime context and returns it as a new function.
bindAll
This method binds all methods of an object (unless specifically stated) to the object.
partial
This method binding binds a function to parameters and then returns it as a new function.
memoize
This method caches the running results of a function for a certain parameter.
delay
This method can postpone the function to run for a specified time.
defer
This method can postpone running the function until the number of tasks to be run reaches 0, similar to the effect of setTimeout delaying running for 0 seconds.
throttle
This method returns a new version of the function. When calling this new version of the function continuously, you must wait for a certain period of time before triggering the next execution.
debounce
This method also returns a new version of a function. Each time this new version of the function is called, there must be a certain amount of time between the previous call, otherwise it will be invalid. Its typical application is to prevent users from double-clicking a button, causing the form to be submitted twice.
once
This method returns a new version of the function so that this function can only be run once. Mainly used for object initialization.
after
This method returns a new version of the function. This function will only run after being called a certain number of times. It is mainly used to confirm that a set of operations are completed before reacting.
wrap
This method takes a function as a parameter, passes it into another function, and finally returns a new version of the former.
compose
This method accepts a series of functions as parameters and runs them in sequence from back to front. The running result of the previous function is used as the running parameter of the next function. In other words, convert the form of f(g(),h()) into f(g(h())).
Tool methods
template
This method is used to compile HTML templates. It accepts three parameters.
templateString: template string
data: input template data
settings: settings
templateString
The template string templateString is an ordinary HTML language, in which variables are inserted in the form of <%= ... %>; the data object is responsible for providing the value of the variable.
<%= word %>
JavaScript commands can be inserted in the form of <% … %>. The following is an example of a judgment statement.
data
All variables in templateString are internally attributes of the obj object, and the obj object refers to the second parameter data object. The following two statements are equivalent.