如何检查JavaScript中的函数是否需要执行参数?如何检查函数是否需要参数?
function functionWithArgs(arg1, arg2) {...}
function functionWithoutArgs() { /* I will return a function here */ }
我在checkin函数是否有通过ES7装饰器的任何参数,所以我不知道检查它们时的函数名称。
内一个装饰
// If the function has arguments, use descriptor.value without invoking it
if (descriptor.value.HAS_ARGUMENTS) descriptor.value;
// If the function has no arguments, invoke it as it will return a function with arguments
else descriptor.value();
虽然我使用ES7装饰,这个问题应该能够在不ES7的任何知识来回答。
更新:我知道,我可以这样做if (descriptor.value() === 'undefined')
,以确定该函数是否返回任何值,但可能并不总是返回函数内的情况。
仅供参考有没有这样的东西作为ES7装饰。 ES2016(ES7)规格已经完成,装饰器仍然只是[第二阶段提案](https://github.com/tc39/proposal-decorators)。 –
你不知道一个函数*是否需要*参数来运行;只是它是否可以接受它们。 – Utkanos
@DanPrince我没有提到我实际上使用的是TypeScript,但我不想将它归类为TypeScript问题以覆盖更广泛的受众。而尼娜的回答对我来说足够好用了。 – borislemke