Creates a new function that when called will set the value of thisArg as the this keyword
value when calling the provided fn instance, and all of the arguments passed to the new
function will be passed along to the original provided instance (after any argArray arguments
which are bound to it).
fnBind is the intrinsic Function.prototype.bind bound to the intrinsic Function.prototype.call, so
fnBind(fn, thisArg, ...argArray) is fn.bind(thisArg, ...argArray) without any wrapper function, and it doesn't
depend on the bind, call or apply properties of fn (which may have been overridden). On a runtime
without the native Function.prototype.bind (eg. ES3) it is a polyfill which returns a function that calls fn
with thisArg and the bound arguments followed by its own arguments, using the intrinsic
Function.prototype.apply (unlike a native bound function it can't be called with new, and it doesn't have the
name and length of fn). Which one is used is resolved once, when the module is loaded.
Since
0.9.8
Param: fn
The function instance to be called
Param: thisArg
The value to be used as the this when calling the fn
Param: argArray
Zero or more arguments to pre-bind to the function
Returns
The new (bound) function, which returns the value returned by the original fn after executing with
the provided thisArg.
// It can also be used to proxy to the original function from the new one module2.getX = fnBind(module1.getX, module1); module2.getX(); // 21
// A less-specific `Function`-typed call site, where the concrete parameter list is not available consthost = { prefix:"Hello", log(value: string) { returnthis.prefix + " " + value; } };
constdynamicLog: Function = host.log; constbound = fnBind(dynamicLog, host);
Creates a new function that when called will set the value of
thisArgas thethiskeyword value when calling the providedfninstance, and all of the arguments passed to the new function will be passed along to the original provided instance (after anyargArrayarguments which are bound to it).fnBindis the intrinsicFunction.prototype.bindbound to the intrinsicFunction.prototype.call, sofnBind(fn, thisArg, ...argArray)isfn.bind(thisArg, ...argArray)without any wrapper function, and it doesn't depend on thebind,callorapplyproperties offn(which may have been overridden). On a runtime without the nativeFunction.prototype.bind(eg. ES3) it is a polyfill which returns a function that callsfnwiththisArgand the bound arguments followed by its own arguments, using the intrinsicFunction.prototype.apply(unlike a native bound function it can't be called withnew, and it doesn't have thenameandlengthoffn). Which one is used is resolved once, when the module is loaded.Since
0.9.8
Param: fn
The function instance to be called
Param: thisArg
The value to be used as the
thiswhen calling thefnParam: argArray
Zero or more arguments to pre-bind to the function
Returns
The new (bound) function, which returns the value returned by the original
fnafter executing with the providedthisArg.Example