apply-or
Invoke .apply if value is a function, otherwise, return default value.
npm install apply-or --save
npm stats
Why?
Function.prototype.apply
is normally sufficient; however, there are situations where it is useful to treat a value as a Function
(invoke it) only if it is indeed a Function
; otherwise, return it as-is. Calling .apply
on a value that is not a function would cause an error.
Examples
delay.js
var apply = { } // does nothing (because argument is not a function) // prints 'Hello, World' to stdout
divmax.js
var apply = { return Mathmax / divisor} var data = val: 9 7 15 12 //=> divmax.apply({ val: [9, 7, 15, 12] }, [5])//=> 15 / 5//=> 3
API
apply(func, args, self)
arguments
func: (Function)
Function to be invoked.args: (Array|*)
Arguments to apply to function.self: (Object)
this
value.
returns
(*)
Result of applying function or default value.