mirror of https://github.com/doocs/leetcode.git
1.8 KiB
1.8 KiB
| comments | difficulty | edit_url | tags | |
|---|---|---|---|---|
| true | 简单 | https://github.com/doocs/leetcode/edit/main/solution/2600-2699/2690.Infinite%20Method%20Object/README.md |
|
2690. 无穷方法对象 🔒
题目描述
请你编写一个函数,返回一个 无穷方法对象 。
无穷方法对象 被定义为一个对象,它允许您调用任何方法,并始终返回方法的名称。
例如,如果执行 obj.abc123() ,它将返回 "abc123" 。
示例 1:
输入:method = "abc123" 输出:"abc123" 解释: const obj = createInfiniteObject(); obj['abc123'](); // "abc123" 返回的字符串应始终与方法名称匹配。
示例 2:
输入:method = ".-qw73n|^2It" 输出:".-qw73n|^2It" 解释:返回的字符串应始终与方法名称匹配。
提示:
0 <= method.length <= 1000
解法
方法一
TypeScript
function createInfiniteObject(): Record<string, () => string> {
return new Proxy(
{},
{
get: (_, prop) => () => prop.toString(),
},
);
}
/**
* const obj = createInfiniteObject();
* obj['abc123'](); // "abc123"
*/