I am going through some code left behind by some long-gone developers and came across this:
Function.prototype.binding = function ()
{
if(arguments.length < 2 && typeof arguments[0] == "undefined") return this;
var __method = this, args = jQuery.makeArray(arguments), object = args.shift();
return function()
{ return __method.apply(object, args.concat(jQuery.makeArray(arguments))); };
};
and it is referenced elsewhere in a couple of places, this DataTables callback function is representitive:
"fnRowCallback": function (nRow, aData, iDisplayIndex)
{
nRow = this.options.fnRowCallback(nRow, aData, iDisplayIndex);
return nRow;
}.binding(this),
I think that what this is accomplishing is to set the this context in the function to the object passed into the .binding; is that correct? And is this way of doing that considered a good practice?
Thanks,
Matthew
Correct. Also it optionally accepts some extra arguments, which get passed into the function at call time. So if you did
obj.f.binding(obj, 1, 2, 3)(4, 5, 6), the arguments received byobj.fwould be1, 2, 3, 4, 5, 6.This method reproduces the standard ECMAScript Fifth Edition method
Function#bind, but is named differently, presumably as it is not completely compliant with the interface defined by that method.