Infinite Method Object - Problem

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 name and it will always return the name of the method as a string.

For example, if you execute obj.abc123(), it should return "abc123". If you call obj.hello(), it should return "hello".

The object should work with any valid JavaScript method name and should handle chaining as well.

Input & Output

Example 1 — Basic Method Call
$ Input: obj.abc123()
Output: "abc123"
💡 Note: The infinite method object intercepts the call to 'abc123' and returns the method name as a string
Example 2 — Different Method Name
$ Input: obj.hello()
Output: "hello"
💡 Note: Any valid JavaScript identifier can be called as a method, returning its name
Example 3 — Complex Method Name
$ Input: obj.myMethod123ABC()
Output: "myMethod123ABC"
💡 Note: Even complex method names with numbers and mixed case work correctly

Constraints

  • Must work with any valid JavaScript identifier
  • Method calls should return the method name as a string
  • Should handle alphanumeric method names
  • No predefined limit on method names

Visualization

Tap to expand
INPUTALGORITHMRESULTMethod Callsobj.abc123()obj.hello()Any valid identifier1Create Proxy Object2Intercept property access3Return function with name4Execute and return stringString Output"abc123""hello""methodName"Method name as stringKey Insight:JavaScript Proxy allows dynamic interception of property access,enabling truly infinite method names without predefinition.TutorialsPoint - Infinite Method Object | JavaScript Proxy Solution
Asked in
Google 15 Facebook 12 Amazon 8
25.0K 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