Javascript 如何触发提前退出外部功能?

Javascript 如何触发提前退出外部功能?,javascript,Javascript,我试图创建一个函数,负责检查布尔值,如果为真,则提前退出并发出警告 以下是我试图实现的目标的示例: function warnAndDie(shouldDie) { if(shouldDie) { console.log("go away, i'm dying!"); // TODO: some code to cause the calling function to exit early } } function triggerTheWarn

我试图创建一个函数,负责检查布尔值,如果为真,则提前退出并发出警告

以下是我试图实现的目标的示例:

function warnAndDie(shouldDie) {
    if(shouldDie) {
        console.log("go away, i'm dying!");
        // TODO: some code to cause the calling function to exit early
    }
}

function triggerTheWarnAndDie() {
    shouldWarnAndDie(true);
    console.log("I should never run!");
}

function dontTriggerTheWarnAndDie() {
    shouldWarnAndDie(false);
    console.log("I should run!");
}
如何才能使
warnAndDie
能够使调用函数提前终止

谢谢

使用

return;
要退出的关键字


*这只是一个快速的解决方法。您可能需要查看错误检查和异常…

您有几个选项。我将为您列出两个非常基本的选项:

  • 返回一个值(可能是布尔值),并根据初始返回值提前从调用方返回

    function shouldWarnAndDie(shouldDie) {
        if(shouldDie) {
            console.log("go away, i'm dying!");
            return true;
        }
        return false;
    }
    
    function triggerTheWarnAndDie() {
        var hasDied = shouldWarnAndDie(true);
        if (hasDied) return;
        console.log("I should never run!");
    }
    
  • 抛出异常

    function shouldWarnAndDie(shouldDie) {
        if(shouldDie) {
            throw "Go away, i'm dying!";
            // or cleaner:
            // throw new dyingException("Go away, i'm dying!");
        }
    }
    
    function triggerTheWarnAndDie() {
       try {
          shouldWarnAndDie(true);
          console.log("I should never run!");
       }
       catch(err) {
          console.log(err+" He's dead Jim!");
       }
    }
    

  • 有更多的高级机制现在可能不在您的范围之内,但是关于回调和承诺的机制绝对值得一看。

    这可以通过基本的
    异常处理来实现。这里我创建了一个自定义异常,可以使用
    try-catch
    语句捕获该异常

    function shouldWarnAndDie(shouldDie) {
        if(shouldDie) {
            throw new DyingException();
        }
    }
    
    function triggerTheWarnAndDie() {
        try {
            shouldWarnAndDie(true);
        } catch(err) {
            console.log(err.message);
            return;
        }
        console.log("I should never run!");
    }
    
    function dontTriggerTheWarnAndDie() {
        try {
            shouldWarnAndDie(false);
        } catch(err) {
            console.log(err.message);
            return;
        }
        console.log("I should run!");
    }
    
    // custom Exception handler
    function DyingException() {
        // do some custom exception handling here
        return new Error("Go away, I am dying!");
    }
    
    triggerTheWarnAndDie();     // Go away, I am dying!
    dontTriggerTheWarnAndDie(); // I should run!
    
    这里有一个

    我建议使用,但并非所有浏览器都支持它。我们可以使用回调,回调中的代码只有在
    warnAndDie
    允许执行时才能执行

    function warnAndDie(shouldDie, fn) {
        if(shouldDie) {
            console.log("go away, i'm dying!");
            // TODO: some code to cause the calling function to exit early
            return;
        }
        fn();
    }
    
    function triggerTheWarnAndDie() {
        shouldWarnAndDie(true, function () {
            console.log("I should never run!");
        } );
    }
    
    function dontTriggerTheWarnAndDie() {
        shouldWarnAndDie(false, function () {
           console.log("I should run!");
        } );
    }
    

    您正在查找异常。您引用了一个名为
    shouldWarnAndDie
    的函数,但未列出该函数。另外,
    return
    是退出函数所需的全部。这不就是从
    warnAndDie
    返回并允许
    触发warnAndDie
    继续执行吗?啊。。。可能返回一个负值(-1),并在
    内触发警告和死亡
    ,检查该值是否为负值,然后
    返回
    中断