A function to get the current host and thisArg that will be called
The name of the function to call on the host
The result of calling the function with the specified this
value and arguments.
const module1 = {
prefix: "Hello",
x: 21,
getX() {
return this.x;
},
log(value: string) {
return this.prefix + " " + value + " : " + this.x
}
};
// The 'this' parameter of 'getX' is bound to 'module'.
module1.getX(); // 21
module1.log("Darkness"); // Hello Darkness : 21
// Create a new function 'boundGetX' with the 'this' parameter bound to 'module'.
let module2 = {
prefix: "my",
x: 42
};
let getHost = () => {
return module1;
};
let deferredFn = createFnDeferredProxy(getHost, "getX");
deferredFn(); // 21
module2.defX = deferredFn;
module2.defX(); // 21
Create a deferred proxy function which will call the named function of the result of the hostFn, this enables creating bound functions which when called call the proxy the function to a different host (this) instance.
This is different from
fnBind
which is provided with the concrete function andthis
instances, while the proxy will lazily obtain thethis
and the function is obtained by looking up the named function from the returned host (this
) instance.