[JavaScript]下面代码的输出结果

这篇博客探讨了一段JavaScript代码的执行过程,并给出了详细的答案和解析,帮助读者理解代码的运行逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    function fun(n,o){
        console.log(o);
         return{
             fun:function(m){
                 return fun(m,n);
             }
         };
    }
    var a= fun(0);
    a.fun(1);
    a.fun(2);
    a.fun(3);
    var b=fun(0).fun(1).fun(2).fun(3);
    var c=fun(0).fun(1);
    c.fun(2);
    c.fun(3);

答案和解析在下面:





















答案:
在这里插入图片描述

解析:

    var obj = new Fun();
    function fun(n,o){
        console.log(o);
         return{
             fun:function(m){
                 return fun(m,n);
             }
         };
    }
    var a= fun(0);
    //此时0作为参数传给n, n=0,o=undefined   输出undefined
    a.fun(1);
    //此时1作为参数传给m,n=0 return fun(m,n)后 n=1,o=0   输出0
    a.fun(2);
    //此时2作为参数传给m,n=0 return fun(m,n)后 n=2,o=0   输出0
    a.fun(3);
    //此时3作为参数传给m,n=0 return fun(m,n)后 n=3,o=0   输出0
    
    var b=fun(0).fun(1).fun(2).fun(3);
    //1.var b=fun(0)  此时0作为参数传给n,  n=0,o=undefined   输出undefined
    //2.var b=fun(0).fun(1) 此时1作为参数传给m,  return fun(m,n)后 n=传进来的m=1,
    //  o=传进来的n=0,输出0
    //3.var b=fun(0).fun(1).fun(2)  此时2作为参数传给m,  return fun(m,n)后 
    //  n=传进来的m=2,o=传进来的n=1,输出1
    //4.var b=fun(0).fun(1).fun(2).fun(3);此时3作为参数传给m,  return fun(m,n)后
    //  n=传进来的m=3,o=传进来的n=2,输出2
    
    
    var c=fun(0).fun(1);//会前面两个就会第三个
    c.fun(2);
    c.fun(3);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值