Skip to main content
Back to problems
#2693
Medium JavaScript

Call function with custom context

78.2% acceptance
Mar 2, 2026
144
14
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.

Solution

TypeScript
Time O(1)
Space O(1)
LeetCode
solution.ts
type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONValue[]
  | { [key: string]: JSONValue };

interface Function {
  callPolyfill(
  context: Record<string, JSONValue>,
  ...args: JSONValue[]
  ): JSONValue;
}

Function.prototype.callPolyfill = function (
  context: Record<string, JSONValue>,
  ...args: JSONValue[]
): JSONValue {
  const sym = Symbol();
  (context as any)[sym] = this;
  const result = (context as any)[sym](...args);
  delete (context as any)[sym];
  return result;
};

/**
 * function increment() { this.count++; return this.count; }
 * increment.callPolyfill({count: 1}); // 2
 */