2754. Bind Function to Context


Problem Description

The task is to implement a method named bindPolyfill that can be called on any JavaScript function. When a function calls bindPolyfill and is passed an object, the method should enable the function to adopt the given object as its this context. This is similar to the native JavaScript Function.bind method, but the problem requires us to implement this functionality manually, without using Function.bind. The bindPolyfill method should return a new function that, when executed, has the passed object as its this context, thus allowing us to pre-set which object should be treated as this when the function is called. The new function should also be able to accept arguments just like the original function.

An example is provided to clarify the expected behavior. Without bindPolyfill, calling the function f directly results in this being undefined, so it outputs "My context is undefined". However, when f is bound to an object with a ctx property using bindPolyfill, and then called, it should output "My context is My Object" since the this context is now the passed object.

The solution needs to work without relying on the built-in Function.bind method, which typically provides this functionality in JavaScript.

Intuition

To solve this problem, we modify the Function prototype, effectively adding the bindPolyfill method to all functions. JavaScript functions are objects too and can have methods. By extending the Function prototype, any function in JavaScript will inherit the bindPolyfill method.

The bindPolyfill method is implemented as a higher-order function – that is, a function that returns another function. Inside bindPolyfill, we return an arrow function that, when called, invokes the original function using the Function.call method. The call method is deliberately used here because it allows us to specify the this context for the function it's called on. The first argument to call is the object we want to set as the this context. Any additional arguments needed by the function are passed through using the spread syntax ...args.

The returned arrow function captures the obj parameter from the bindPolyfill's scope (creates a closure), so when it is later invoked as the bound function, it still has access to obj to use as the this context. Thus, when the bound function is called, it executes with the correct this context regardless of how it's called, preserving the binding created by bindPolyfill.

Solution Approach

The solution approach involves the use of prototype-based inheritance in JavaScript and the concept of closures to achieve the functionality similar to what Function.bind offers. Here's a step-by-step breakdown of the approach based on the provided TypeScript solution code:

  1. Prototype Extension: We extend the Function prototype by adding a new method called bindPolyfill. This means that every function in JavaScript will now have this method available for use.

    1interface Function {
    2    bindPolyfill(obj: Record<any, any>): Fn;
    3}
  2. Implementing bindPolyfill: The bindPolyfill function is defined as a property of Function.prototype.

    1Function.prototype.bindPolyfill = function (obj) {
    2    // ...
    3};
  3. Closure Creation: Inside the bindPolyfill method, we return an arrow function. This arrow function is created in the scope of bindPolyfill, which means it has ongoing access to the obj parameter even after bindPolyfill has finished execution. This phenomenon where a function has access to the scope in which it was created is known as a "closure."

    1return (...args) => {
    2    // ...
    3};
  4. The use of call Method: In the returned arrow function, we use the Function.call method. call allows us to explicitly set the this context for the function when it's invoked which is the object passed to bindPolyfill.

    1return this.call(obj, ...args);

By leveraging these aspects of JavaScript, the bindPolyfill method effectively allows us to bind a this context to a function without using the built-in Function.bind method. When the returned function (created by the closure) is later invoked, the original function is called with the specified this and any passed arguments, ensuring the expected context and behavior.

Ready to land your dream job?

Unlock your dream job with a 2-minute evaluator for a personalized learning plan!

Start Evaluator

Example Walkthrough

Let's walk through an example that illustrates how the bindPolyfill solution approach works. Assume we have an object user and a function showUsername that should print out a username based on the this context it is given.

1const user = {
2    username: 'JavaScriptFan',
3};
4
5function showUsername() {
6    console.log(`The username is: ${this.username}`);
7}

Without bindPolyfill or Function.bind, if we try to call showUsername directly, it will not have the user object as its context, and this.username would be undefined.

Now, let's use the bindPolyfill method on our showUsername function and pass it the user object. We expect the new function we get back to have the user object as its this context.

1// Define our bindPolyfill function based on the provided solution approach
2Function.prototype.bindPolyfill = function (obj) {
3    return (...args) => {
4        return this.call(obj, ...args);
5    };
6};
7
8// We create a bound function using our polyfill
9const showUsernameBound = showUsername.bindPolyfill(user);
10
11// Now, when we call the bound function...
12showUsernameBound(); // The output will be: "The username is: JavaScriptFan"

Here's what happens step-by-step:

  1. We add the bindPolyfill method to Function.prototype, making it available to all functions.
  2. Inside bindPolyfill, we define an arrow function that captures the user object (as obj) in a closure.
  3. When showUsernameBound() is called, the arrow function uses Function.call to execute showUsername with this set to the user object.
  4. The showUsername function now has the correct this context and prints "The username is: JavaScriptFan" to the console, which confirms that our bindPolyfill works as expected.

This example demonstrates how bindPolyfill can be used to set the this context of a function to a specific object, mimicking the behavior of the native Function.bind method.

Solution Implementation

1from types import FunctionType
2from typing import Any, Callable, Dict
3
4# Define a callable type that can take any number of arguments and return any type.
5Fn = Callable[..., Any]
6
7def bind_polyfill(func: FunctionType, obj: Dict[str, Any]) -> Fn:
8    """
9    Takes a function and an object, returning a new function bound to the object.
10
11    Parameters:
12    func: The original function to bind.
13    obj: A dictionary representing the object to bind to the original function.
14
15    Returns:
16    A new function with `self` bound to the provided object `obj`.
17    """
18
19    # Closure captures the `func` and `obj` to create a bound function.
20    def bound_function(*args, **kwargs) -> Any:
21        """
22        This inner function is the actual bound function to be returned.
23        It takes any number of positional and keyword arguments.
24
25        Returns:
26        The result of the original function called with the bound object
27        and the provided arguments.
28        """
29
30        # Bind the `obj` to `self` and call `func` with it and other arguments.
31        return func(obj, *args, **kwargs)
32
33    # Return the closured bound function.
34    return bound_function
35
36# Example usage to extend the functionality of an existing function.
37# Assuming we have a function `some_func` and object `some_obj` defined,
38# we could create a bound function like:
39# bound_some_func = bind_polyfill(some_func, some_obj)
40# bound_some_func(arg1, arg2) would call `some_func(some_obj, arg1, arg2)`
41
1import java.util.function.Function;
2
3// Functional interface representing a function that can take any number of arguments and return any type.
4@FunctionalInterface
5interface VarArgsFunction {
6    Object apply(Object... args);
7}
8
9// Extend the Function interface to augment with the bindPolyfill method.
10interface BoundFunction extends Function<Object[], Object> {
11    // Static method to create a bound function.
12    static BoundFunction bindPolyfill(Function<Object[], Object> function, Object obj) {
13        // Return a new function that, when invoked, will have its context 'this' set to the provided object.
14        return (args) -> function.apply(obj, args);
15    }
16}
17
18// This class demonstrates how to use the BoundFunction interface with the bindPolyfill method.
19public class FunctionBinder {
20
21    public static void main(String[] args) {
22        // Instantiate a new function that accepts an array of objects and could return any object.
23        Function<Object[], Object> myFunction = (args) -> {
24            // example implementation using the first argument and adjusting it.
25            if (args != null && args.length > 0 && args[0] instanceof String) {
26                return ((String) args[0]).toUpperCase();
27            }
28            return null;
29        };
30
31        // Context object for binding the 'this' reference.
32        Object context = new Object();
33
34        // Using the static method bindPolyfill to bind 'myFunction' to 'context'.
35        BoundFunction boundFunction = BoundFunction.bindPolyfill(myFunction, context);
36
37        // Call the bound function with arguments.
38        Object result = boundFunction.apply(new Object[]{"hello"});
39
40        // Example usage of the result.
41        System.out.println(result); // Outputs: HELLO
42    }
43}
44
1#include <functional>
2#include <string>
3#include <map>
4
5// Now we define a functional type (Fn) that can take any number of arguments
6// and return any type by using templates.
7template<typename ReturnType, typename... Args>
8using Fn = std::function<ReturnType(Args...)>;
9
10// Since we can't directly augment global interfaces like in TypeScript,
11// we create a class that will simulate the binding functionality.
12class FunctionPolyfill {
13public:
14    // In C++, we can't have a variadic return type, so we fix the return type to void
15    // for simplicity. If we need another return type, we'd have to define another template.
16    template<typename... Args>
17    static Fn<void, Args...> bindPolyfill(Fn<void, Args...> func, std::map<std::string, any> obj) {
18        // We return a lambda function that captures 'func' and 'obj' by value.
19        return [func, obj](Args... args) -> void {
20            // Inside the lambda, we assume that we don't need to pass 'obj' to 'func',
21            // since 'obj' is just to simulate the `this` context from JavaScript,
22            // and we don't have that concept in the same way in C++.
23            func(std::forward<Args>(args)...);
24        };
25    }
26};
27
28// Usage example:
29void exampleFunction(int a) {
30    // Some function logic here...
31}
32
33int main() {
34    // Create a FunctionPolyfill object and bind 'exampleFunction'
35    auto boundExampleFunction = FunctionPolyfill::bindPolyfill(exampleFunction, {});
36  
37    // Call the bound function
38    boundExampleFunction(42);
39  
40    return 0;
41}
42
1// Define a functional type that can take any number of arguments and return any type.
2type Fn = (...args: any[]) => any;
3
4// Augment the global Function interface with the bindPolyfill method.
5declare global {
6    interface Function {
7        // The bindPolyfill method takes an object and returns a bound function.
8        bindPolyfill(this: Function, obj: Record<string, any>): Fn;
9    }
10}
11
12// Add the bindPolyfill method to the prototype of the Function object.
13Function.prototype.bindPolyfill = function(this: Function, obj: Record<string, any>): Fn {
14    // Return a new function that, when called, has its `this` keyword set to the provided object.
15    return (...args: any[]): any => {
16        // Call the original function with `this` bound to `obj` and with the provided arguments.
17        return this.call(obj, ...args);
18    };
19};
20

Time and Space Complexity

Time Complexity

The bindPolyfill function is essentially creating a closure that captures the this context along with an object and any supplied arguments at a later time. The function does not, in and of itself, include iterations or recursive calls, hence its time complexity is O(1) (constant time), as creating the closure and returning a new function is done in a constant amount of steps regardless of the size of the input.

Space Complexity

The space complexity for the bindPolyfill function also equates to O(1) (constant space), since it only involves creating a single new function regardless of the input size. It does not allocate additional space that grows with the size of the input arguments. However, it should be noted that each bound function created by this method will occupy space, and if many such bound functions are created, the total space used in the program would cumulatively increase.


Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:
Question 1 out of 10

You are given an array of intervals where intervals[i] = [start_i, end_i] represent the start and end of the ith interval. You need to merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input.


Recommended Readings

Got a question? Ask the Monster Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

Coding Interview Strategies

Dive into our free, detailed pattern charts and company guides to understand what each company focuses on.

See Patterns

🪄