我有如下两个功能:

var doSomething = function() {
    // first check if user wants to proceed and some other restrictions
    checkIfReallyShouldProceed();
    console.log('ok, proceed');
    // proceed and do something
}

var checkIfReallyShouldProceed = function() {
    var check = confirm('really proceed?');
    if(!check){
    //stop executing doSomething
    }
}

doSomething();


如果用户不确定,我想从doSomething返回。当然,我可以将check变量的结果返回给doSomething并得到类似

if(!checkIfReallyShouldProceed()){
  return;
}


在那里,但我希望被调用函数停止执行调用函数。这可能吗?如果可以,怎么办?

最佳答案

为此类型的条件过程创建一个if条件:

var doSomething = function() {
    if (checkIfReallyShouldProceed()){
      return true; // This will stop the doSomething function from executing
    }

    console.log('ok, proceed');
}

var checkIfReallyShouldProceed = function() {
    return confirm('really proceed?'); // returns true/false
}

doSomething();


checkIfReallyShouldProceed函数中,返回用户是否要继续。在doSomething中,如果被调用的方法返回true,它将停止执行。

关于javascript - 从Java调用函数返回,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32613845/

10-13 00:28