Infinite Method Object - Problem
Create an Infinite Method Object

You need to write a function that returns a special object with a unique property: any method you call on it will return the name of that method as a string.

This "infinite method object" should dynamically handle method calls that don't actually exist, using JavaScript's powerful meta-programming capabilities. When you call obj.someMethod(), it should return "someMethod". When you call obj.abc123(), it should return "abc123".

Goal: Return an object that can respond to any method name and return that method name as a string.

Example:
const obj = createInfiniteObject();
obj.hello() // returns "hello"
obj.getData() // returns "getData"
obj.method123() // returns "method123"

Input & Output

basic_usage.js โ€” Basic Method Calls
$ Input: const obj = createInfiniteObject(); obj.hello() obj.getData() obj.test123()
โ€บ Output: "hello" "getData" "test123"
๐Ÿ’ก Note: The infinite object returns the exact method name that was called, regardless of what the method name is.
complex_names.js โ€” Complex Method Names
$ Input: const obj = createInfiniteObject(); obj.getUserById() obj.calculateTotalPrice() obj.method_with_underscores() obj.camelCaseMethod()
โ€บ Output: "getUserById" "calculateTotalPrice" "method_with_underscores" "camelCaseMethod"
๐Ÿ’ก Note: The object handles any valid JavaScript property name, including camelCase, snake_case, and mixed formats.
edge_cases.js โ€” Edge Cases
$ Input: const obj = createInfiniteObject(); obj['special-method']() obj['123numeric']() obj.$dollarMethod() obj._privateMethod()
โ€บ Output: "special-method" "123numeric" "$dollarMethod" "_privateMethod"
๐Ÿ’ก Note: Even special characters and bracket notation work, as the Proxy intercepts all property access patterns.

Visualization

Tap to expand
USERPROXYReceptionistIntercepts allmethod callsTARGETEmpty ObjectNever reacheddirectlyobj.hello()Returns Function:() => "hello"RESULT"hello"
Understanding the Visualization
1
Method Call Made
User calls obj.someMethod() - like asking receptionist for 'John'
2
Proxy Intercepts
Proxy catches the call before it reaches the object - like receptionist hearing the request
3
Dynamic Response
Proxy returns a function that will return 'someMethod' - like receptionist saying 'connecting you to John'
4
Function Executes
The returned function is called and returns the method name - like completing the connection
Key Takeaway
๐ŸŽฏ Key Insight: JavaScript's Proxy acts like a universal interceptor that can handle any property access dynamically, making it perfect for creating objects with infinite methods without consuming memory for unused methods.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Each method call is intercepted and handled in constant time

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only stores the proxy handler, no matter how many methods are called

n
2n
โœ“ Linear Space

Constraints

  • The function must return an object that works with any valid JavaScript property name
  • Method calls should return the method name as a string
  • The solution should handle an infinite number of different method names
  • Method names can contain letters, numbers, underscores, and dollar signs
  • Must work with both obj.methodName() and obj['method-name']() syntax
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
28.4K 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