从另一个函数中的函数返回

我有这种情况:


    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但我想知道是否有另一种方法来解决这个问题)


交互式爱情
浏览 117回答 3
3回答

慕桂英3389331

如果 a 为空,我不希望 f1 继续使用 console.log()在这种情况下,您必须测试函数a内的值f1:function f1(a) {        a = f2(a);        // do some other stuff                if (a == 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)

慕后森

如果是,您可以存储 的最后一个值a并退出。tempnullfunction 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);

MYYA

只返回假    function f1(a) {        a = f2(a);        // do some other stuff        if (!a) return        console.log("I don't want the function f1 to log this.")    }        function f2(a) {        if (a == null) {            console.log("enters here")            return false;        }        return a;    }        f1(null)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript