apply-or
Invoke .apply if value is a function, otherwise, return default value.
$ npm install apply-or --save
Why?
Function.prototype.bind
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.
Example
var apply = require('apply-or');
function divmax (divisor) {
return Math.max.apply(null, this.val) / divisor
}
var data = {
val: [9, 7, 15, 12]
}
apply(divmax, 5, data)
//=> divmax.apply({ val: [9, 7, 15, 12] }, [5])
//=> 15 / 5
//=> 3
API
apply(func, args, self)
func
Function to be invoked.args
Arguments to apply to function.self
this
value.
Licenses