2693. Call Function with Custom Context LeetCode Solution
In this guide, you will get 2693. Call Function with Custom Context LeetCode Solution with the best time and space complexity. The solution to Call Function with Custom Context problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Call Function with Custom Context solution in C++
- Call Function with Custom Context solution in Java
- Call Function with Custom Context solution in Python
- Additional Resources

Problem Statement of Call Function with Custom Context
Enhance all functions to have the callPolyfill method. The method accepts an object obj as its first parameter and any number of additional arguments. The obj becomes the this context for the function. The additional arguments are passed to the function (that the callPolyfill method belongs on).
For example if you had the function:
function tax(price, taxRate) {
const totalCost = price * (1 + taxRate);
console.log(`The cost of ${this.item} is ${totalCost}`);
}
Calling this function like tax(10, 0.1) will log “The cost of undefined is 11”. This is because the this context was not defined.
However, calling the function like tax.callPolyfill({item: “salad”}, 10, 0.1) will log “The cost of salad is 11”. The this context was appropriately set, and the function logged an appropriate output.
Please solve this without using the built-in Function.call method.
Example 1:
Input:
fn = function add(b) {
return this.a + b;
}
args = [{“a”: 5}, 7]
Output: 12
Explanation:
fn.callPolyfill({“a”: 5}, 7); // 12
callPolyfill sets the “this” context to {“a”: 5}. 7 is passed as an argument.
Example 2:
Input:
fn = function tax(price, taxRate) {
return `The cost of the ${this.item} is ${price * taxRate}`;
}
args = [{“item”: “burger”}, 10, 1.1]
Output: “The cost of the burger is 11”
Explanation: callPolyfill sets the “this” context to {“item”: “burger”}. 10 and 1.1 are passed as additional arguments.
Constraints:
typeof args[0] == ‘object’ and args[0] != null
1 <= args.length <= 100
2 <= JSON.stringify(args[0]).length <= 105
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
2693. Call Function with Custom Context LeetCode Solution in C++
type JSONValue =
| null
| boolean
| number
| string
| JSONValue[]
| { [key: string]: JSONValue };
declare global {
interface Function {
callPolyfill(
context: Record<string, JSONValue>,
...args: JSONValue[]
): JSONValue;
}
}
Function.prototype.callPolyfill = function (context, ...args): JSONValue {
const fn = this;
Object.defineProperty(context, '__fn__', {
value: fn,
enumerable: false,
});
const ans = (context.__fn__ as any)(...args);
delete context.__fn__;
return typeof ans !== 'undefined' ? ans : undefined;
};
/* code provided by PROGIEZ */
2693. Call Function with Custom Context LeetCode Solution in Java
N/A
// code provided by PROGIEZ
2693. Call Function with Custom Context LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.