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
Goal: Return an object that can respond to any method name and return that method name as a string.
Example:
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
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
โ Linear Growth
Space Complexity
O(1)
Only stores the proxy handler, no matter how many methods are called
โ 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()andobj['method-name']()syntax
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code