为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

交互式爱情 2022-05-26 17:01:25
我有这种情况:    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但我想知道是否有另一种方法来解决这个问题)
查看完整描述

3 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

如果 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)


查看完整回答
反对 回复 2022-05-26
?
慕后森

TA贡献1802条经验 获得超5个赞

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


查看完整回答
反对 回复 2022-05-26
?
MYYA

TA贡献1868条经验 获得超4个赞

只返回假


    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)


查看完整回答
反对 回复 2022-05-26
  • 3 回答
  • 0 关注
  • 116 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号