Skip to main content
Back to problems
#2690
Easy JavaScript

Infinite method object

93.1% acceptance
Mar 31, 2026
32
9
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".

Solution

TypeScript
Time O(1)
Space O(1)
LeetCode
solution.ts
function createInfiniteObject(): Record<string, () => string> {
  return new Proxy({}, {
    get(_, prop: string) {
      return () => prop;
    }
  }) as Record<string, () => string>;
};

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