_.after(n, func)

Creates a function that executes func, with the this binding and arguments of the created function, only after being called n times.

Arguments

  1. n (number): The number of times the function must be called before func is executed.
  2. func (Function): The function to restrict.

Returns

(Function): Returns the new restricted function.

Example

  1. var saves = ['profile', 'settings'];
  2. var done = _.after(saves.length, function() {
  3. console.log('Done saving!');
  4. });
  5. _.forEach(saves, function(type) {
  6. asyncSave({ 'type': type, 'complete': done });
  7. });
  8. // => logs 'Done saving!', after all saves have completed

_.bind(func, [thisArg], [arg])

Creates a function that, when called, invokes func with the this binding of thisArg and prepends any additional bind arguments to those provided to the bound function.

Arguments

  1. func (Function): The function to bind.
  2. [thisArg] (*): The this binding of func.
  3. [arg] (…*): Arguments to be partially applied.

Returns

(Function): Returns the new bound function.

Example

  1. var func = function(greeting) {
  2. return greeting + ' ' + this.name;
  3. };
  4. func = _.bind(func, { 'name': 'fred' }, 'hi');
  5. func();
  6. // => 'hi fred'

_.bindAll(object, [methodName])

Binds methods of an object to the object itself, overwriting the existing method. Method names may be specified as individual arguments or as arrays of method names. If no method names are provided all the function properties of object will be bound.

Arguments

  1. object (Object): The object to bind and assign the bound methods to.
  2. [methodName] (…string): The object method names to bind, specified as individual method names or arrays of method names.

Returns

(Object): Returns object.

Example

  1. var view = {
  2. 'label': 'docs',
  3. 'onClick': function() { console.log('clicked ' + this.label); }
  4. };
  5. _.bindAll(view);
  6. jQuery('#docs').on('click', view.onClick);
  7. // => logs 'clicked docs', when the button is clicked

_.bindKey(object, key, [arg])

Creates a function that, when called, invokes the method at object[key] and prepends any additional bindKey arguments to those provided to the bound function. This method differs from _.bind by allowing bound functions to reference methods that will be redefined or don’t yet exist. See http://michaux.ca/articles/lazy-function-definition-pattern.

Arguments

  1. object (Object): The object the method belongs to.
  2. key (string): The key of the method.
  3. [arg] (…*): Arguments to be partially applied.

Returns

(Function): Returns the new bound function.

Example

  1. var object = {
  2. 'name': 'fred',
  3. 'greet': function(greeting) {
  4. return greeting + ' ' + this.name;
  5. }
  6. };
  7. var func = _.bindKey(object, 'greet', 'hi');
  8. func();
  9. // => 'hi fred'
  10. object.greet = function(greeting) {
  11. return greeting + 'ya ' + this.name + '!';
  12. };
  13. func();
  14. // => 'hiya fred!'

_.compose([func])

Creates a function that is the composition of the provided functions, where each function consumes the return value of the function that follows. For example, composing the functions f(), g(), and h() produces f(g(h())). Each function is executed with the this binding of the composed function.

Arguments

  1. [func] (…Function): Functions to compose.

Returns

(Function): Returns the new composed function.

Example

  1. var realNameMap = {
  2. 'pebbles': 'penelope'
  3. };
  4. var format = function(name) {
  5. name = realNameMap[name.toLowerCase()] || name;
  6. return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();
  7. };
  8. var greet = function(formatted) {
  9. return 'Hiya ' + formatted + '!';
  10. };
  11. var welcome = _.compose(greet, format);
  12. welcome('pebbles');
  13. // => 'Hiya Penelope!'

_.curry(func, [arity=func.length])

Creates a function which accepts one or more arguments of func that when invoked either executes func returning its result, if all func arguments have been provided, or returns a function that accepts one or more of the remaining func arguments, and so on. The arity of func can be specified if func.length is not sufficient.

Arguments

  1. func (Function): The function to curry.
  2. [arity=func.length] (number): The arity of func.

Returns

(Function): Returns the new curried function.

Example

  1. var curried = _.curry(function(a, b, c) {
  2. console.log(a + b + c);
  3. });
  4. curried(1)(2)(3);
  5. // => 6
  6. curried(1, 2)(3);
  7. // => 6
  8. curried(1, 2, 3);
  9. // => 6

_.debounce(func, wait, [options])

Creates a function that will delay the execution of func until after wait milliseconds have elapsed since the last time it was invoked. Provide an options object to indicate that func should be invoked on the leading and/or trailing edge of the wait timeout. Subsequent calls to the debounced function will return the result of the last func call.

Note: If leading and trailing options are true func will be called on the trailing edge of the timeout only if the the debounced function is invoked more than once during the wait timeout.

Arguments

  1. func (Function): The function to debounce.
  2. wait (number): The number of milliseconds to delay.
  3. [options] (Object): The options object.
  4. [options.leading=false] (boolean): Specify execution on the leading edge of the timeout.
  5. [options.maxWait] (number): The maximum time func is allowed to be delayed before it’s called.
  6. [options.trailing=true] (boolean): Specify execution on the trailing edge of the timeout.

Returns

(Function): Returns the new debounced function.

Example

  1. // avoid costly calculations while the window size is in flux
  2. var lazyLayout = _.debounce(calculateLayout, 150);
  3. jQuery(window).on('resize', lazyLayout);
  4. // execute `sendMail` when the click event is fired, debouncing subsequent calls
  5. jQuery('#postbox').on('click', _.debounce(sendMail, 300, {
  6. 'leading': true,
  7. 'trailing': false
  8. });
  9. // ensure `batchLog` is executed once after 1 second of debounced calls
  10. var source = new EventSource('/stream');
  11. source.addEventListener('message', _.debounce(batchLog, 250, {
  12. 'maxWait': 1000
  13. }, false);

_.defer(func, [arg])

Defers executing the func function until the current call stack has cleared. Additional arguments will be provided to func when it is invoked.

Arguments

  1. func (Function): The function to defer.
  2. [arg] (…*): Arguments to invoke the function with.

Returns

(number): Returns the timer id.

Example

  1. _.defer(function(text) { console.log(text); }, 'deferred');
  2. // logs 'deferred' after one or more milliseconds

_.delay(func, wait, [arg])

Executes the func function after wait milliseconds. Additional arguments will be provided to func when it is invoked.

Arguments

  1. func (Function): The function to delay.
  2. wait (number): The number of milliseconds to delay execution.
  3. [arg] (…*): Arguments to invoke the function with.

Returns

(number): Returns the timer id.

Example

  1. _.delay(function(text) { console.log(text); }, 1000, 'later');
  2. // => logs 'later' after one second

_.memoize(func, [resolver])

Creates a function that memoizes the result of func. If resolver is provided it will be used to determine the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the cache key. The func is executed with the this binding of the memoized function. The result cache is exposed as the cache property on the memoized function.

Arguments

  1. func (Function): The function to have its output memoized.
  2. [resolver] (Function): A function used to resolve the cache key.

Returns

(Function): Returns the new memoizing function.

Example

  1. var fibonacci = _.memoize(function(n) {
  2. return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  3. });
  4. fibonacci(9)
  5. // => 34
  6. var data = {
  7. 'fred': { 'name': 'fred', 'age': 40 },
  8. 'pebbles': { 'name': 'pebbles', 'age': 1 }
  9. };
  10. // modifying the result cache
  11. var get = _.memoize(function(name) { return data[name]; }, _.identity);
  12. get('pebbles');
  13. // => { 'name': 'pebbles', 'age': 1 }
  14. get.cache.pebbles.name = 'penelope';
  15. get('pebbles');
  16. // => { 'name': 'penelope', 'age': 1 }

_.once(func)

Creates a function that is restricted to execute func once. Repeat calls to the function will return the value of the first call. The func is executed with the this binding of the created function.

Arguments

  1. func (Function): The function to restrict.

Returns

(Function): Returns the new restricted function.

Example

  1. var initialize = _.once(createApplication);
  2. initialize();
  3. initialize();
  4. // `initialize` executes `createApplication` once

_.partial(func, [arg])

Creates a function that, when called, invokes func with any additional partial arguments prepended to those provided to the new function. This method is similar to _.bind except it does not alter the this binding.

Arguments

  1. func (Function): The function to partially apply arguments to.
  2. [arg] (…*): Arguments to be partially applied.

Returns

(Function): Returns the new partially applied function.

Example

  1. var greet = function(greeting, name) { return greeting + ' ' + name; };
  2. var hi = _.partial(greet, 'hi');
  3. hi('fred');
  4. // => 'hi fred'

_.partialRight(func, [arg])

This method is like _.partial except that partial arguments are appended to those provided to the new function.

Arguments

  1. func (Function): The function to partially apply arguments to.
  2. [arg] (…*): Arguments to be partially applied.

Returns

(Function): Returns the new partially applied function.

Example

  1. var defaultsDeep = _.partialRight(_.merge, _.defaults);
  2. var options = {
  3. 'variable': 'data',
  4. 'imports': { 'jq': $ }
  5. };
  6. defaultsDeep(options, _.templateSettings);
  7. options.variable
  8. // => 'data'
  9. options.imports
  10. // => { '_': _, 'jq': $ }

_.throttle(func, wait, [options])

Creates a function that, when executed, will only call the func function at most once per every wait milliseconds. Provide an options object to indicate that func should be invoked on the leading and/or trailing edge of the wait timeout. Subsequent calls to the throttled function will return the result of the last func call.

Note: If leading and trailing options are true func will be called on the trailing edge of the timeout only if the the throttled function is invoked more than once during the wait timeout.

Arguments

  1. func (Function): The function to throttle.
  2. wait (number): The number of milliseconds to throttle executions to.
  3. [options] (Object): The options object.
  4. [options.leading=true] (boolean): Specify execution on the leading edge of the timeout.
  5. [options.trailing=true] (boolean): Specify execution on the trailing edge of the timeout.

Returns

(Function): Returns the new throttled function.

Example

  1. // avoid excessively updating the position while scrolling
  2. var throttled = _.throttle(updatePosition, 100);
  3. jQuery(window).on('scroll', throttled);
  4. // execute `renewToken` when the click event is fired, but not more than once every 5 minutes
  5. jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
  6. 'trailing': false
  7. }));

_.wrap(value, wrapper)

Creates a function that provides value to the wrapper function as its first argument. Additional arguments provided to the function are appended to those provided to the wrapper function. The wrapper is executed with the this binding of the created function.

Arguments

  1. value (*): The value to wrap.
  2. wrapper (Function): The wrapper function.

Returns

(Function): Returns the new function.

Example

  1. var p = _.wrap(_.escape, function(func, text) {
  2. return '<p>' + func(text) + '</p>';
  3. });
  4. p('Fred, Wilma, & Pebbles');
  5. // => '<p>Fred, Wilma, &amp; Pebbles</p>'