Utility Flashcards
_.noConflict()
Give control of the _ variable back to its previous owner. Returns a reference to the Underscore object.
_.identity(value)
Returns the same value that is used as the argument. In math: f(x) = x This function looks useless, but is used throughout Underscore as a default iteratee.
_.constant(value)
Creates a function that returns the same value that is used as the argument of _.constant.
_.noop()
Returns undefined irrespective of the arguments passed to it. Useful as the default for optional callback arguments.
_.times(n, iteratee, [context])
Invokes the given iteratee function n times. Each invocation of iteratee is called with an index argument. Produces an array of the returned values. Note: this example uses the chaining syntax.
_.random(min, max)
Returns a random integer between min and max, inclusive. If you only pass one argument, it will return a number between 0 and that number.
_.mixin(object)
Allows you to extend Underscore with your own utility functions. Pass a hash of {name: function} definitions to have your functions added to the Underscore object, as well as the OOP wrapper.
_.iteratee(value, [context])
A mostly-internal function to generate callbacks that can be applied to each element in a collection, returning the desired result — either identity, an arbitrary callback, a property matcher, or a property accessor. The full list of Underscore methods that transform predicates through _.iteratee is map, find, filter, reject, every, some, max, min, sortBy, groupBy, indexBy, countBy, sortedIndex, partition, and unique.
_.uniqueId([prefix])
Generate a globally-unique id for client-side models or DOM elements that need one. If prefix is passed, the id will be appended to it.
_.escape(string)
Escapes a string for insertion into HTML, replacing &, , “, `, and ‘ characters.
_.unescape(string)
The opposite of escape, replaces &, , “, ` and ‘ with their unescaped counterparts.
_.result(object, property, [defaultValue])
If the value of the named property is a function then invoke it with the object as context; otherwise, return it. If a default value is provided and the property doesn’t exist or is undefined then the default will be returned. If defaultValue is a function its result will be returned.
_.now()
Returns an integer timestamp for the current time, using the fastest method available in the runtime. Useful for implementing timing/animation functions.
_.template(templateString, [settings])
Compiles JavaScript templates into functions that can be evaluated for rendering. Useful for rendering complicated bits of HTML from JSON data sources. Template functions can both interpolate values, using , as well as execute arbitrary JavaScript code, with . If you wish to interpolate a value, and have it be HTML-escaped, use . When you evaluate a template function, pass in a data object that has properties corresponding to the template’s free variables. The settings argument should be a hash containing any _.templateSettings that should be overridden.
_.chain(obj)
Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.