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] != null1 <= args.length <= 1002 <= JSON.stringify(args[0]).length <= 105The first approach involves augmenting the Function prototype to add our custom callPolyfill method. This method should simulate the behavior of the native Function.prototype.call method. We'll save the original context of this, invoke our function with the given context, and apply the additional arguments.
In this solution, we temporarily assign the function to be called as a property of the context object using a symbol to avoid potential property name clashes. We then call the function using this set context and pass the function arguments using the spread operator. After calling the function, we delete the temporary property to clean up.
Python
Time Complexity: O(1) — The time to set a property and invoke a function is constant.
Space Complexity: O(1) — Space used is constant because no structures grow with input size.
This approach leverages closures to encapsulate the function call logic. By using an immediately invoked function expression (IIFE), we can create a local scope that captures the context and arguments for the function call.
This solution wraps the call to this.apply within an arrow function, effectively using a closure to capture the current function, context, and arguments. The function is immediately invoked to simulate a direct call.
Java
Time Complexity: O(1) — Function invocation is a constant time operation.
Space Complexity: O(1) — No additional space is required.
| Approach | Complexity |
|---|---|
| Approach 1: Modify Function Constructor | Time Complexity: O(1) — The time to set a property and invoke a function is constant. |
| Approach 2: Using Closures | Time Complexity: O(1) — Function invocation is a constant time operation. |
Letter Combinations of a Phone Number - Backtracking - Leetcode 17 • NeetCode • 186,659 views views
Watch 9 more video solutions →Practice Call Function with Custom Context with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor