Creates a new function that when called will set the value of thisArg
as the this
keyword
value whrn calling the provided fn
instance, and all of the arguments passed to the new
function will be passed along to the original provided instance.
The value returned by the original fn
after executing with the provided thisArg
.
const module1 = {
x: 21,
getX() {
return this.x;
},
};
// The 'this' parameter of 'getX' is bound to 'module'.
console.log(module1.getX()); // 21
// Create a new function 'boundGetX' with the 'this' parameter bound to 'module'.
let module2 = {
x: 42
};
module2.getX = fnBind(module1.getX, module2);
module2.getX(); // 42
// It can also be used to proxy to the original function from the new one
module2.getX = fnBind(module1.getX, module1);
module2.getX(); // 21
Creates a new function that when called will set the value of
thisArg
as thethis
keyword value whrn calling the providedfn
instance, and all of the arguments passed to the new function will be passed along to the original provided instance.Param: fn
The function instance to be called
Param: thisArg
The value to be used as the
this
when calling thefn
Returns
The value returned by the original
fn
after executing with the providedthisArg
.Since
0.9.8
Example