Skip to content

2690. Infinite Method Object πŸ”’

Description

Write a function thatΒ returns anΒ infinite-methodΒ object.

AnΒ infinite-methodΒ objectΒ is defined as an object that allows you to call any method and it will always return the name of the method.

For example, if you executeΒ obj.abc123(), it will returnΒ "abc123".

Β 

Example 1:

Input: method = "abc123"
Output: "abc123"
Explanation:
const obj = createInfiniteObject();
obj['abc123'](); // "abc123"
The returned string should always match the method name.

Example 2:

Input: method = ".-qw73n|^2It"
Output: ".-qw73n|^2It"
Explanation: The returned string should always match the method name.

Β 

Constraints:

  • 0 <= method.length <= 1000

Solutions

Solution 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function createInfiniteObject(): Record<string, () => string> {
    return new Proxy(
        {},
        {
            get: (_, prop) => () => prop.toString(),
        },
    );
}

/**
 * const obj = createInfiniteObject();
 * obj['abc123'](); // "abc123"
 */

Comments