首页 > 解决方案 > 从另一个函数中的函数返回

问题描述

我有这种情况:

    function f1(a) {
        a = f2(a);
        // do some other stuff
        console.log("I don't want the function f1 to log this.")
    }
    
    function f2(a) {
        if (a == null) {
            console.log("enters here")
            return;
        }
        return a;
    }
    
    f1(null)

如果a为空,我不想f1继续使用console.log(). 我可以改变什么来获得这种行为?(我知道我可以用一些来做到这一点,booleans但我想知道是否有另一种方法来解决这个问题)

标签: javascript

解决方案


如果是,您可以存储 的最后一个值a并退出。tempnull

function f1(a) {
    let temp = a;
    a = f2(a);
    // do some other stuff
    if (temp === null) return;
    console.log("I don't want the function f1 to log this.");
}

function f2(a) {
    if (a == null) {
        console.log("enters here");
        return;
    }
    return a;
}

f1(null);


推荐阅读