function MyFunction () {
if (SomeCondition) {
MyInnerFunction(SomeParam);
return;
}
if (SomeOtherCondition) {
MyInnerFunction(SomeOtherParam);
return;
}
if (SomeThirdCondition) {
MyInnerFunction(AnotherParam);
return;
}
function MyInnerFunction(Param) {
// Do some work here
// HERE: I want return from MyFunction
}
}
如您所见,当MyInnerFunction
返回时,要执行的下一个语句是return
MyFunction
语句。有没有办法消除所有这些return语句,以便MyFunction
内的MyInnerFunction
返回执行?
感谢。
答案 0 :(得分:3)
您可以重新格式化代码,以便if条件仅修改参数。 然后你只需在最后用正确的参数调用你的方法
function MyFunction () {
var params;
if (SomeCondition)
params = SomeParam;
else if (SomeOtherCondition)
params = SomeOtherParam;
else if (SomeThirdCondition)
params = AnotherParam;
MyInnerFunction(params);
}