如何优雅地为数组中列出且即将执行/调用的函数和方法提供 try-catch 功能?

sre*_*moh -3 javascript arrays metaprogramming function try-catch

我有这个数组:

const arr = [
  { 'Some text': this.f('a') },
  { 'Some other text': this.f('b') }
]
Run Code Online (Sandbox Code Playgroud)

为了实现这一目标,我编写了实验代码。

arr.forEach((entry) => {
  console.log(Object.entries(entry));
})
Run Code Online (Sandbox Code Playgroud)

结果它执行了我的功能:

[[ 'Some text': 'result_f1' ]]
[[ 'Some other text': 'result_f2' ]]
Run Code Online (Sandbox Code Playgroud)

我的函数自动执行了。

但我不认为这是实现这一目标的最安全方法。我想更明确地分别执行每个函数并尝试捕获每个函数。

我想这样做的原因是将每个函数包装到一个单独的try-catch块中,因为在数组中进行尝试捕获会破坏代码的可读性

有什么想法如何实现这一目标吗?

T.J*_*der 6

这些函数不会“自动”执行,它们之所以被执行是因为您显式调用了它们:

\n
const arr = [\n    { \'Some text\': this.f(\'a\') },\n    //             ^^^^^^^^^^^\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92 here\n    { \'Some other text\': this.f(\'b\') }\n    //                   ^^^^^^^^^^^\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92 and here\n]\n
Run Code Online (Sandbox Code Playgroud)\n

上面的结果是一个包含两个对象的数组,其中第一个对象有一个调用的属性,其Some text值是调用的结果this.f(\'a\'),第二个对象有一个调用的属性Some other text,其值是调用的结果this.f(\'b\')

\n

如果您想将这些调用延迟到稍后的时间,您需要将它们包装在函数中:

\n
const arr = [\n    { \'Some text\': () => this.f(\'a\') },\n    //             ^^^^^^^^^^^^^^^^^\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92 wrapper function\n    { \'Some other text\': () => this.f(\'b\') }\n    //                   ^^^^^^^^^^^^^^^^^\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92\xe2\x88\x92 wrapper function\n]\n
Run Code Online (Sandbox Code Playgroud)\n

稍后,您可以这样称呼它们:

\n
arr[0]["Some text"]();\narr[0]["Some other tex"]();\n
Run Code Online (Sandbox Code Playgroud)\n

或类似的。

\n

try如果你想稍后在/块中调用它们catch,你可以这样做:

\n
for (const obj of arr) {\n    for (const fn of Object.values(obj)) {\n        try {\n            fn();\n        } catch (e) {\n            // ...\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

...或等效于forEach

\n
arr.forEach(obj => {\n    Object.values(obj).forEach(fn => {\n        try {\n            fn();\n        } catch (e) {\n            // ...\n        }\n    });\n});\n
Run Code Online (Sandbox Code Playgroud)\n

在另一条评论中你说过:

\n
\n

如何用函数结果替换函数?

\n
\n

我怀疑你想要这样的东西:

\n
const arr = [\n    { \'Some text\': () => this.f(\'a\') },\n    { \'Some other text\': () => this.f(\'b\') }\n].map(obj => {\n    return Object.fromEntries(Object.entries(obj).map(([key, value]) => {\n        try {\n            value = value();\n        } catch (e) {\n            // ...presumably do something with the fact it failed...\n        }\n        return [key, value];\n    }));\n});\n
Run Code Online (Sandbox Code Playgroud)\n

上面的结果是对象具有与初始对象文字相同的键,但值是调用函数(或在块中arr写入的任何内容)的结果。valuecatch

\n

看:

\n\n