Call Function with Custom Context - Problem

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.

Input & Output

Example 1 — Tax Calculator with Context
$ Input: function tax(price, taxRate) { return price * (1 + taxRate); }, context = {item: "salad"}, args = [10, 0.1]
Output: 11
💡 Note: The tax function is called with 'this' set to {item: "salad"}, calculating 10 * (1 + 0.1) = 11
Example 2 — Greeting Function
$ Input: function greet(name) { return `Hello ${name}, I am ${this.title}`; }, context = {title: "Dr."}, args = ["Alice"]
Output: "Hello Alice, I am Dr."
💡 Note: The greet function accesses this.title from the provided context object
Example 3 — Multiple Arguments
$ Input: function sum(a, b, c) { return a + b + c + this.offset; }, context = {offset: 100}, args = [1, 2, 3]
Output: 106
💡 Note: Function sums arguments 1+2+3=6, then adds this.offset=100, resulting in 106

Constraints

  • 1 ≤ args.length ≤ 100
  • 0 ≤ JSON.stringify(obj).length ≤ 105
  • Function must work without using built-in call method

Visualization

Tap to expand
Call Function with Custom Context INPUT function tax(price, taxRate) return price * (1 + taxRate); } context (obj) {item: "salad"} args (additional params) [10, 0.1] Call Structure: tax.callPolyfill(obj, 10, 0.1) price=10 rate=0.1 this=obj ALGORITHM STEPS 1 Create Unique Symbol const sym = Symbol(); 2 Attach Function to obj obj[sym] = this; // the fn 3 Call via obj Property result = obj[sym](...args) 4 Cleanup and Return delete obj[sym]; return result Symbol Binding Process obj item:"salad" [sym]: tax tax() this = obj FINAL RESULT callPolyfill Implementation Function.prototype .callPolyfill = function(obj, ...args) { const sym = Symbol(); obj[sym] = this; const res = obj[sym] (...args); delete obj[sym]; return res; }; Calculation 10 * (1 + 0.1) = 10 * 1.1 = 11 OUTPUT 11 OK - Context bound correctly! Key Insight: Symbol-based Property Assignment Using a Symbol as a temporary property key on the context object allows us to bind the function without risk of collision with existing properties. When called as obj[sym](...args), JavaScript automatically sets 'this' to obj. This avoids using the built-in Function.call or Function.apply. TutorialsPoint - Call Function with Custom Context | Symbol-based Property Assignment
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
23.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen