Call Function with Custom Context Polyfill

20.0% Acceptance

In this lab, you will build a custom implementation of the method by creating a callPolyfill method. This will allow you to set the this context within a function and pass any additional arguments to that function.

The callPolyfill method should accept an object obj as its first parameter and any number of additional arguments. The obj will become the this context for the function, and the additional arguments are passed to the function that the callPolyfill method belongs to.

For example, let's say you have the following 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" 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.

Your task is to implement the callPolyfill method without using the built-in 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, 0.1] // Output: "The cost of the burger is 1" // Explanation: callPolyfill sets the "this" context to {"item": "burger"}. 10 and 0.1 are passed as additional arguments.


  • typeof args[0] == 'object' && args[0] !== null
  • 1 <= args.length <= 100
  • 2 <= JSON.stringify(args[0]).length <= 105


  1. Implement the callPolyfill method
  2. Test the callPolyfill method to ensure it sets the this context correctly and accepts additional arguments
  3. Make sure you default export the function

Good luck, and happy coding!