xbind
Sugar way to adapt the arguments signature of functions.
Usage
var xbind = ;var path = ; { var ret = ; for var i = 0 len = argumentslength; i < len; i++ ret; return ret;} console// `arrayify` just make an array from arguments [ 0, 1, 2, 3 ] console// use the first argument [ 0 ] console// use the first argument [ 0 ] console// use the last argument [ 3 ] console// use the first and third arguments [ 0, 2 ] [ 0, 2 ] console// append arguments 4,5 [ 0, 1, 2, 3, 4, 5 ] console// prepend arguments 4,5 [ 4, 5, 0, 1, 2, 3 ] console// slice arguments 1,3 [ 1, 2 ] console// filter arguments [ 1 ]
xfn = xbind(fn, ctx)
Return a new function with a xargs
property, through which arguments can be changed.
fn
Type: Function
, String
Function to be bound. If String
, it will be treated as a method name of ctx
ctx
Type: Object
Optional
this
value of fn
when called.
If not specified, this
will be this
of xfn
.
operations
xfn.xargs(index1, index2)
Select arguments with index1, index2
xfn.first()
Select the first argument
xfn.last()
Select the last argument
Array methods
You can operate the argument list using .push
, .pop
, .shift
, .unshift
, .slice
, .splice
, .filter
, .map
, .reduce
, as the argument list is Array
.
xbind.identity(o)
Just return a function that returns o