goog.functions
Functions
EMPTY() → void
void
Always returns undefined
(loosely-typed version).
FALSE( arg0 ) → boolean
boolean
NULL( arg0 ) → null
null
TRUE( arg0 ) → boolean
boolean
UNDEFINED( arg0 ) → void
void
and( ...var_args ) → function(...?): boolean
function(...?): boolean
Creates a function that returns true if each of its components evaluates to true. The components are evaluated in order, and the evaluation will be short-circuited as soon as a function returns false. For example, (goog.functions.and(f, g))(x) is equivalent to f(x) && g(x).
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
cacheReturnValue<T>( fn ) → function(): T
function(): T
Gives a wrapper function that caches the return value of a parameterless function when first called.
When called for the first time, the given function is called and its return value is cached (thus this is only appropriate for idempotent functions). Subsequent calls will return the cached return value. This allows the evaluation of expensive functions to be delayed until first used.
To cache the return values of functions with parameters, see goog.memoize.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
compose<T>( fn, ...var_args ) → function(...?): T
function(...?): T
Creates the composition of the functions passed in. For example, (goog.functions.compose(f, g))(a) is equivalent to f(g(a)).
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
constant<T>( retValue ) → function(): T
function(): T
Creates a function that always returns the same value.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
create<T>( constructor, ...var_args ) → T
T
Generic factory function to construct an object given the constructor and the arguments. Intended to be bound to create object factories.
Example:
var factory = goog.partial(goog.functions.create, Class);
warning Deprecated | This function does not work with ES6 class constructors. Use arrow functions + spread args instead. |
---|
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
debounce<SCOPE>( f, interval, opt_scope ) → function(...?): undefined
function(...?): undefined
Wraps a function to allow it to be called, at most, once per interval (specified in milliseconds). If the wrapper function is called N times within that interval, only the Nth call will go through.
This is particularly useful for batching up repeated actions where the
last action should win. This can be used, for example, for refreshing an
autocomplete pop-up every so often rather than updating with every keystroke,
since the final text typed by the user is the one that should produce the
final autocomplete results. For more stateful debouncing with support for
pausing, resuming, and canceling debounced actions, use
goog.async.Debouncer
.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
equalTo( value, opt_useLooseComparison ) → function(*): boolean
function(*): boolean
Creates a function that returns whether its argument equals the given value.
Example: var key = goog.object.findKey(obj, goog.functions.equalTo('needle'));
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
error( message ) → Function
Function
Creates a function that always throws an error with the given message.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
fail( err ) → Function
Function
Creates a function that throws the given object.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
identity<T>( opt_returnValue, ...var_args ) → T
T
A simple function that returns the first argument of whatever is passed into it.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
isFunction( val ) → boolean
boolean
Returns true if the specified value is a function.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
lock( f, opt_numArgs ) → Function
Function
Given a function, create a function that keeps opt_numArgs arguments and silently discards all additional arguments.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
not( f ) → function(...?): boolean
function(...?): boolean
Creates a function that returns the Boolean opposite of a provided function. For example, (goog.functions.not(f))(x) is equivalent to !f(x).
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
nth( n ) → Function
Function
Creates a function that returns its nth argument.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
once( f ) → function(): undefined
function(): undefined
Wraps a function to allow it to be called, at most, once. All additional calls are no-ops.
This is particularly useful for initialization functions that should be called, at most, once.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
or( ...var_args ) → function(...?): boolean
function(...?): boolean
Creates a function that returns true if any of its components evaluates to true. The components are evaluated in order, and the evaluation will be short-circuited as soon as a function returns true. For example, (goog.functions.or(f, g))(x) is equivalent to f(x) || g(x).
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
partialRight( fn, ...var_args ) → Function
Function
Like goog.partial(), except that arguments are added after arguments to the returned function.
Usage: function f(arg1, arg2, arg3, arg4) { ... } var g = goog.functions.partialRight(f, arg3, arg4); g(arg1, arg2);
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
rateLimit<SCOPE>( f, interval, opt_scope ) → function(...?): undefined
function(...?): undefined
Wraps a function to allow it to be called, at most, once per interval (specified in milliseconds). If the wrapper function is called N times within that interval, only the 1st call will go through.
This is particularly useful for limiting repeated user requests where the first request is guaranteed to have all the data required to perform the final action, so there's no need to wait until the end of the interval before sending the request out.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
sequence( ...var_args ) → Function
Function
Creates a function that calls the functions passed in in sequence, and returns the value of the last function. For example, (goog.functions.sequence(f, g))(x) is equivalent to f(x),g(x).
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
throttle<SCOPE>( f, interval, opt_scope ) → function(...?): undefined
function(...?): undefined
Wraps a function to allow it to be called, at most, once per interval (specified in milliseconds). If the wrapper function is called N times in that interval, both the 1st and the Nth calls will go through.
This is particularly useful for limiting repeated user requests where the the last action should win, but you also don't want to wait until the end of the interval before sending a request out, as it leads to a perception of slowness for the user.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
withReturnValue<T>( f, retValue ) → function(...?): T
function(...?): T
Given a function, create a new function that swallows its return value and replaces it with a new one.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
Compiler Constants
goog.functions.CACHE_RETURN_VALUE → boolean
boolean
Whether the return value cache should be used. This should only be used to disable caches when testing.