You can define another function, let's call it partial
that binds some extra arguments to your function:
function partial( func : Function, ...boundArgs ) : Function {
return function( ...dynamicArgs ) : * {
return func.apply(null, boundArgs.concat(dynamicArgs))
}
}
Then you change your function like this:
private function formatDate( dataField : String, item : Object, column : DataGridColumn ) : String {
var df : DateFormatter = new DateFormatter();
df.formatString = "MM/DD/YY";
if ( column.dataField == dataField ) {
return df.format(item[dataField]);
}
return "ERR";
}
Notice that I have added a new argument called dataField
first in the argument list, and replaced all references to "startDate" with that argument.
And use it like this:
var startDateLabelFunction : Function = partial(formatDate, "startDate");
var endDateLabelFunction : Function = partial(formatDate, "endDate");
The partial
function returns a new function that calls the original function with the parameters from the call to partial concatenated with the parameters to the new function... you with me? Another way of putting it is that it can return a new function where N of the arguments are pre-bound to specific values.
Let's go through it step by step:
partial(formatDate, "startDate")
returns a function that looks like this:
function( ...dynamicArgs ) : * {
return func.apply(null, boundArgs.concat(dynamicArgs));
}
but the func
and boundArgs
are what you passed as arguments to partial
, so you could say that it looks like this:
function( ...dynamicArgs ) : * {
return formatDate.apply(null, ["startDate"].concat(dynamicArgs));
}
which, when it is called, will be more or less the same as this
function( item : Object, column : DataGridColumn ) : * {
return formatDate("startDate", item, column);
}
Tada!