Utility Flashcards

1
Q

_.noConflict()

A

Give control of the _ variable back to its previous owner. Returns a reference to the Underscore object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

_.identity(value)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

_.constant(value)

A

Creates a function that returns the same value that is used as the argument of _.constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

_.noop()

A

Returns undefined irrespective of the arguments passed to it. Useful as the default for optional callback arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

_.times(n, iteratee, [context])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

_.random(min, max)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

_.mixin(object)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

_.iteratee(value, [context])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

_.uniqueId([prefix])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

_.escape(string)

A

Escapes a string for insertion into HTML, replacing &, , “, `, and ‘ characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

_.unescape(string)

A

The opposite of escape, replaces &, , “, ` and ‘ with their unescaped counterparts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

_.result(object, property, [defaultValue])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

_.now()

A

Returns an integer timestamp for the current time, using the fastest method available in the runtime. Useful for implementing timing/animation functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

_.template(templateString, [settings])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

_.chain(obj)

A

Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

_(obj).value()

A

Extracts the value of a wrapped object.

17
Q

Give control of the _ variable back to its previous owner. Returns a reference to the Underscore object.

A

_.noConflict()

18
Q

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.

A

_.identity(value)

19
Q

Creates a function that returns the same value that is used as the argument of _.constant.

A

_.constant(value)

20
Q

Returns undefined irrespective of the arguments passed to it. Useful as the default for optional callback arguments.

A

_.noop()

21
Q

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.

A

_.times(n, iteratee, [context])

22
Q

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.

A

_.random(min, max)

23
Q

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.

A

_.mixin(object)

24
Q

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.

A

_.iteratee(value, [context])

25
Q

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.

A

_.uniqueId([prefix])

26
Q

Escapes a string for insertion into HTML, replacing &, , “, `, and ‘ characters.

A

_.escape(string)

27
Q

The opposite of escape, replaces &, , “, ` and ‘ with their unescaped counterparts.

A

_.unescape(string)

28
Q

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.

A

_.result(object, property, [defaultValue])

29
Q

Returns an integer timestamp for the current time, using the fastest method available in the runtime. Useful for implementing timing/animation functions.

A

_.now()

30
Q

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.

A

_.template(templateString, [settings])

31
Q

Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.

A

_.chain(obj)

32
Q

Extracts the value of a wrapped object.

A

_(obj).value()